Android通过MediaStore获取缩略图模糊

问题描述

文件管理器中图片和视频的缩略图很模糊,实际打开图片或视频文件很清晰

问题分析

获取缩略图的方法

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用于视频缩略图。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值