安卓如何获取已知路径的SD卡存储图片的缩略图

17

假设我有一张存储在路径"/mnt/images/abc.jpg"下的图片,我该如何获取系统生成的缩略图位图。我知道如何从Uri获取图像的缩略图,但不知道如何从文件路径获取。


1
您可以使用Java的ThumbnailUtil类轻松创建缩略图视频和图像。Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height); - sujith s
5个回答

24

你可以试试这个:

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;

}

1
谢谢您先生!显而易见,但有人可能需要它,如果您想显示更大的图像,请使用MediaStore.Images.Thumbnails.MINI_KIND。 - keybee
我找到了最好的解决方案。非常感谢,这应该是被接受的答案。 - Tunerx
1
MINI_KIND 用于:512 x 384 缩略图 MICRO_KIND 用于:96 x 96 缩略图 - Ashish Saini

4

您可以通过以下方式从文件中获取Uri

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

以下的函数可以为您提供缩略图:

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);     
}

8
我认为使用MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong(_imageUri.getLastPathSegment()),type, null);会是一个更好的选择,因为系统大部分时间会缓存缩略图。 - pankajagarwal
2
你获取URI的方法似乎不正确,因为对于路径为"/mnt/sdcard/2011-12-02 11.22.50.jpg"的文件,实际的URI是content://media/external/images/media/556,而Uri.fromFile(...)方法返回的是file:///mnt/sdcard/2011-12-02%2011.22.50.jpg。 - pankajagarwal

4

作为其他人已经在他们的答案中提到的替代方法,但我发现获取缩略图的简单方法是使用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);

在某些情况下,它不起作用。并不比从图像存储库中获取(如接受的答案所示)更好。 - Huy Tower
当 BitmapFactory 无法解码字节数组时,才会出现这种情况。 - dharmendra

2
您可以使用Java的ThumbnailUtil类轻松创建缩略图视频和图片。
Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);


 public static Bitmap createVideoThumbnail (String filePath, int kind)

API level 8引入 为视频创建一个缩略图。如果视频损坏或格式不受支持,则可能返回null。

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

有关Thumbnail Util类的更多源代码

Developer.android.com


0
使用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倍。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接