您的位置:首页 > 移动开发 > Android开发

Android加载缩略图的几种方法

2016-02-05 13:14 375 查看
转载请注明出处:/article/10113229.html

最近一段时间做图片缩略图的加载,发现网上的信息杂乱无章,掺杂着许多无效信息,浪费许多时间,经过大量的搜索与实践,整理出来一些能正常使用的方法。

通过MediaStore获取缩略图

Cursor cursor2 = MediaStore.Images.Thumbnails.query(// 查询数据库中所有缩略图
mActivity.getContentResolver(), MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, STORE_THUMB_IMAGES);


PS:系统不会自动生成缩略图

如何让系统生成缩略图(没有找到明确的官方说明,但测试可用)

1、
android.provider.MediaStore.Images.Thumbnails. getThumbnail(ContentResolver cr, long origId, int kind, BitmapFactory.Options options)


2、
MediaScannerConnection.scanFile(context, paths, mimeTypes, callback);


BitmapFactory创建缩略图

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
float realWidth = options.outWidth;
float realHeight = options.outHeight;
int scale = (int) ((realHeight > realWidth ? realHeight : realWidth) / 100);
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(path, options);


ThumbnailUtils加载缩略图

ThumbnailUtils.extractThumbnail(source, width, height) //source为Bitmap

ThumbnailUtils.createVideoThumbnail(filePath, kind);

采用框架加载缩略图

Universal-Image-Loader:资历比较老,使用较为广泛。有比较多的参数可以配置,对图片压缩实在无力。

Picasso:使用较为简单,仅需一句代码便能实现功能。图片质量较高,库的体积小。但不能加载视频缩略图。内存开销大于Glide。

Glide:使用与Picasso类似,图片加载速度较快,可以加载视频缩略图,可以加载gif动图,功能较多。是google推荐的图片加载框架。但库的体积大,占用缓存大于Picasso,图片质量差。

音乐缩略图加载路径

String uri = "content://media/external/audio/media/" + id + "/albumart";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: