问题描述
文件管理器中图片和视频的缩略图很模糊,实际打开图片或视频文件很清晰
问题分析
获取缩略图的方法
Bitmap thumbnail = Images.Thumbnails.getThumbnail(mContext.getContentResolver(), id, Images.Thumbnails.MICRO_KIND, null);
该方法为系统方法getThumbnail
/**
* Return thumbnail representing a specific image item. If a
* thumbnail doesn't exist, this method will block until it's
* generated. Callers are responsible for their own in-memory
* caching of returned values.
*
* As of {@link android.os.Build.VERSION_CODES#Q}, this output
* of the thumbnail has correct rotation, don't need to rotate
* it again.
*
* @param imageId the image item to obtain a thumbnail for.
* @param kind optimal thumbnail size desired.
* @return decoded thumbnail, or {@code null} if problem was
* encountered.
* @deprecated Callers should migrate to using
* {@link ContentResolver#loadThumbnail}, since it
* offers richer control over requested thumbnail sizes
* and cancellation behavior.
*/
@Deprecated
public static Bitmap getThumbnail(ContentResolver cr, long imageId, int kind,
BitmapFactory.Options options) {
final Uri uri = ContentUris.withAppendedId(
Images.Media.EXTERNAL_CONTENT_URI, imageId);
return InternalThumbnails.getThumbnail(cr, uri, kind, options);
}
观察到参数@param kind optimal thumbnail size desired
packages/providers/MediaProvider/apex/framework/java/android/provider/MediaStore.java
public static class ThumbnailConstants {
public static final int MINI_KIND = 1;
public static final int FULL_SCREEN_KIND = 2;
public static final int MICRO_KIND = 3;
public static final Size MINI_SIZE = new Size(512, 384);
public static final Size FULL_SCREEN_SIZE = new Size(1024, 786);
public static final Size MICRO_SIZE = new Size(96, 96);
public static @NonNull Size getKindSize(int kind) {
if (kind == ThumbnailConstants.MICRO_KIND) {
return ThumbnailConstants.MICRO_SIZE;
} else if (kind == ThumbnailConstants.FULL_SCREEN_KIND) {
return ThumbnailConstants.FULL_SCREEN_SIZE;
} else if (kind == ThumbnailConstants.MINI_KIND) {
return ThumbnailConstants.MINI_SIZE;
} else {
throw new IllegalArgumentException("Unsupported kind: " +
}
}
}
因此,如要修改缩略图清晰度,可以修改第三个参数为MINI_KIND或FULL_SCREEN_KIND即可。
关于该参数的显示上 MICRO_KIND较小且呈方形,而 MINI_KIND比较大,呈长方形,可以在获取到缩略图后再自定义形状,另外MediaStore是区分Images和Video的,请记住作为公约使用:
MediaStore.Images.Thumbnails.MINI_KIND用于图像缩略图,MediaStore.Video.Thumbnails.MINI_KIND用于视频缩略图。