Example #1
0
  /**
   * 获取图片缩略图
   *
   * @param dbId
   * @return
   */
  public MediaThumbnail getImageThumbnail(int dbId) {
    String volumeName = "external";
    String selection = Images.Thumbnails.IMAGE_ID + "='" + dbId + "'";
    String[] columns =
        new String[] {Images.Thumbnails.DATA, Images.Thumbnails.WIDTH, Images.Thumbnails.HEIGHT};
    Uri uri = Images.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, Images.Thumbnails.DATA);
      thumbnail.width = MediaDbUtil.getInt(cur, Images.Thumbnails.WIDTH);
      thumbnail.height = MediaDbUtil.getInt(cur, Images.Thumbnails.HEIGHT);
      thumbnail.dbId = dbId;
      thumbnail.type = MediaThumbnail.TYPE_IMAGE;
      return thumbnail;
    } finally {
      if (cur != null) {
        cur.close();
      }
    }
  }
Example #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;
  }
  /**
   * A copy of the Android internals insertImage method, this method populates the meta data with
   * DATE_ADDED and DATE_TAKEN. This fixes a common problem where media that is inserted manually
   * gets saved at the end of the gallery (because date is not populated).
   *
   * @see android.provider.MediaStore.Images.Media#insertImage(ContentResolver, Bitmap, String,
   *     String)
   */
  public static final String insertImage(
      ContentResolver cr, Bitmap source, String title, String description) {

    ContentValues values = new ContentValues();
    values.put(Images.Media.TITLE, title);
    values.put(Images.Media.DISPLAY_NAME, title);
    values.put(Images.Media.DESCRIPTION, description);
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    // Add the date meta data to ensure the image is added at the front of the gallery
    values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());

    Uri url = null;
    String stringUrl = null; /* value to be returned */

    try {
      url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

      if (source != null) {
        OutputStream imageOut = cr.openOutputStream(url);
        try {
          source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
        } finally {
          imageOut.close();
        }

        long id = ContentUris.parseId(url);
        // Wait until MINI_KIND thumbnail is generated.
        Bitmap miniThumb =
            Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null);
        // This is for backward compatibility.
        storeThumbnail(cr, miniThumb, id, 50F, 50F, Images.Thumbnails.MICRO_KIND);
      } else {
        cr.delete(url, null, null);
        url = null;
      }
    } catch (Exception e) {
      if (url != null) {
        cr.delete(url, null, null);
        url = null;
      }
    }

    if (url != null) {
      stringUrl = url.toString();
    }

    return stringUrl;
  }