/**
   * 获取视频缩略图
   *
   * @param dbId
   * @return
   */
  public MediaThumbnail getVideoThumbnail(int dbId) {
    String volumeName = "external";
    String selection = Video.Thumbnails.VIDEO_ID + "='" + dbId + "'";
    String[] columns =
        new String[] {Video.Thumbnails.DATA, Video.Thumbnails.WIDTH, Video.Thumbnails.HEIGHT};
    Uri uri = Video.Thumbnails.getContentUri(volumeName);
    Cursor cur = null;
    try {
      cur = mContext.getContentResolver().query(uri, columns, selection, null, null);
      if (null == cur) {
        return null;
      }

      if (!cur.moveToFirst()) {
        return null;
      }

      MediaThumbnail thumbnail = new MediaThumbnail();
      thumbnail.path = MediaDbUtil.getString(cur, Video.Thumbnails.DATA);
      thumbnail.width = MediaDbUtil.getInt(cur, Video.Thumbnails.WIDTH);
      thumbnail.height = MediaDbUtil.getInt(cur, Video.Thumbnails.HEIGHT);
      thumbnail.dbId = dbId;
      thumbnail.type = MediaThumbnail.TYPE_VIDEO;
      return thumbnail;
    } finally {
      if (cur != null) {
        cur.close();
      }
    }
  }
Exemple #2
0
  public static Thumbnail getLastThumbnail(ContentResolver resolver, boolean isImage) {
    /*
     * Media image = getLastImageThumbnail(resolver); Media video =
     * getLastVideoThumbnail(resolver); Log.e("testthum",
     * "get last thumbnail called"); if (image == null && video == null)
     * return null; if (video == null) Log.e("testthum", "video is null");
     * Bitmap bitmap = null; Media lastMedia; // If there is only image or
     * video, get its thumbnail. If both exist, // get the thumbnail of the
     * one that is newer. if (image != null && (video == null ||
     * image.dateTaken >= video.dateTaken)) { Log.e("testthum",
     * "get thumbnail image"); bitmap =
     * Images.Thumbnails.getThumbnail(resolver, image.id,
     * Images.Thumbnails.MINI_KIND, null); lastMedia = image; } else {
     * Log.e("testthum", "get thumbnail video"); bitmap =
     * Video.Thumbnails.getThumbnail(resolver, video.id,
     * Video.Thumbnails.MINI_KIND, null); lastMedia = video; }
     */

    Media media = isImage ? getLastImageThumbnail(resolver) : getLastVideoThumbnail(resolver);
    if (media == null) {
      Log.d("dyb", "media is null");
      return null;
    }
    Bitmap bitmap = null;
    // If there is only image or video, get its thumbnail. If both exist,
    // get the thumbnail of the one that is newer.
    if (isImage) {
      File dir = new File(Storage.DIRECTORY);
      dir.mkdir();
      if (!dir.isDirectory() || !dir.canWrite()) {
        Log.d("dyb", "dir error");
        return null;
      }
      if (dir.listFiles().length == 0) {
        Log.d("mk", "dir.listFiles().length = " + dir.listFiles().length);
        return null;
      }
      bitmap =
          Images.Thumbnails.getThumbnail(resolver, media.id, Images.Thumbnails.MINI_KIND, null);
    } else {
      File dir = new File(Storage.DIRECTORY);
      dir.mkdir();
      if (!dir.isDirectory() || !dir.canWrite()) {
        Log.d("dyb", "dir error");
        return null;
      }
      if (dir.listFiles().length == 0) {
        Log.d("mk", "dir.listFiles().length = " + dir.listFiles().length);
        return null;
      }
      bitmap = Video.Thumbnails.getThumbnail(resolver, media.id, Video.Thumbnails.MINI_KIND, null);
    }

    // Ensure database and storage are in sync.
    if (Util.isUriValid(media.uri, resolver)) {
      return createThumbnail(media.uri, bitmap, media.orientation);
    }
    return null;
  }