【问题标题】:android get thumbnail of image stored on sdcard whose path is knownandroid获取存储在路径已知的sdcard上的图像的缩略图
【发布时间】:2012-01-13 01:53:51
【问题描述】:

假设我有一个图像存储在路径"/mnt/images/abc.jpg"。如何获取系统生成的此图像的缩略图位图。我知道如何从它的 Uri 中获取图像的缩略图,而不是从它的文件路径中获取图像的缩略图

【问题讨论】:

  • 您可以使用Java的ThumnailUtil类简单地创建缩略图视频和图像 Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);

标签: android thumbnails


【解决方案1】:

你可以试试这个:

public static Bitmap getThumbnail(ContentResolver cr, String path) throws Exception {

    Cursor ca = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.MediaColumns._ID }, MediaStore.MediaColumns.DATA + "=?", new String[] {path}, null);
    if (ca != null && ca.moveToFirst()) {
        int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID));
        ca.close();
        return MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MICRO_KIND, null );
    }

    ca.close();
    return null;

}

【讨论】:

  • 谢谢您,先生!很明显,但有人可能需要它,如果你想显示更大的图像,请使用 MediaStore.Images.Thumbnails.MINI_KIND
  • 我找到的最佳解决方案。非常感谢,这应该是被接受的答案。
  • MINI_KIND 用于:512 x 384 缩略图 MICRO_KIND 用于:96 x 96 缩略图
【解决方案2】:

您可以从这样的文件中获取Uri

Uri uri = Uri.fromFile(new File("/mnt/images/abc.jpg"));
Bitmap thumbnail = getPreview(uri);

以下function 为您提供缩略图:

Bitmap getPreview(Uri uri) {
    File image = new File(uri.getPath());

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
        return null;

    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
    return BitmapFactory.decodeFile(image.getPath(), opts);     
}

【讨论】:

  • 我认为使用MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong(_imageUri.getLastPathSegment()), type, null); 会是更好的选择,因为系统大部分时间都会缓存缩略图
  • 您获取 uri 的方法似乎无法正常工作,因为对于路径为“/mnt/sdcard/2011-12-02 11.22.50.jpg”的文件,实际的 uri 存在:/ /media/external/images/media/556 其中 Uri.fromFile(...) 方法返回 file:///mnt/sdcard/2011-12-02%2011.22.50.jpg
【解决方案3】:

这可能是其他人在他们的回答中已经提到的另一种方法,但我发现获取缩略图的简单方法是使用ExifInterface

    ExifInterface exif = new ExifInterface(pictureFile.getPath());
    byte[] imageData=exif.getThumbnail();
    if (imageData!=null) //it can not able to get the thumbnail for very small images , so better to check null
    Bitmap  thumbnail= BitmapFactory.decodeByteArray(imageData,0,imageData.length);

【讨论】:

  • 在某些情况下,它不起作用。不比从接受的答案中获得的图像商店更好。
  • Yaa 当 BitmapFactory 无法解码字节数组时。
【解决方案4】:

您可以使用 java 的 ThumnailUtil 类简单地创建缩略图视频和图像

Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);


 public static Bitmap createVideoThumbnail (String filePath, int kind)

在 API 级别 8 中添加 为视频创建视频缩略图。如果视频损坏或格式不受支持,可能会返回 null。

参数 filePath 视频文件的路径 kind 可以是 MINI_KIND 或 MICRO_KIND

For more Source code of Thumbnail Util class

Developer.android.com

【讨论】:

    【解决方案5】:

    使用 android Bitmap 类 及其 createScaledBitmap 方法。

    方法描述:

    public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)
    

    使用示例:

    Bitmap bitmap = BitmapFactory.decodeFile(img_path);
    int origWidth = bitmap.getWidth();
    int origHeight = bitmap.getHeight();
    bitmap = Bitmap.createScaledBitmap(bitmap, origWidth / 10, origHeight / 10, false);
    

    根据需要减少源文件。在我的示例中,我将其减少了 10 倍。

    【讨论】:

      猜你喜欢
      • 2011-07-29
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2015-02-17
      • 2013-08-13
      相关资源
      最近更新 更多