参考文献:
缩略图ThumbnailUtils
缩略图和原图

本文目的:读取相册获取缩略图和原图,至于怎么在UI中呈现,本文不做讲解。

  1. 读取相册需要动态获取权限Manifest.permission.READ_EXTERNAL_STORAGE,使用EasyPermissions便可解决

  2. 读取相册列表ContentResolver,读取内容包括:标题,地址,名称,大小,以及对应的id,至于更多类容可以参考projection的字段,例如分类的BUCKET_ID

 String[] projection = { 
 MediaStore.Images.Media.DATA, 
 MediaStore.Images.Media.TITLE, 
 MediaStore.Images.Media.DISPLAY_NAME, 
 MediaStore.Images.Media.SIZE,
 MediaStore.Images.Media._ID};
                    
 ContentResolver cr = getContentResolver();
 Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
 if(cursor != null && cursor.moveToFirst()){
 	do {
 		//获取图片路径
        int photodataIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String path = cursor.getString(photodataIndex);
        //获取图片名称 eg:hello.jpg
        int photoNameIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
        String name = cursor.getString(photoNameIndex);
        //获取图片标题 eg:hello    
        int photoTitleIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE);                
        String title = cursor.getString(photoTitleIndex);
        //获取图片大小     
        int photoSizeIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
        int size = cursor.getInt(photoSizeIndex);
        //获取图片的id                        
        int photoIdIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
        int id = cursor.getInt(photoIdIndex);
        //获取图片的缩略图                    
        Bitmap bitmap = getImageThumbnail(dataItem._path,200,200);


       }while (cursor.moveToNext());

上面省略了一个重要的自定义函数getImageThumbnail,这个函数的功能是根据图片的路径创建缩略图。此时可能部分同学会选择使用Thumbnails(已废弃)表来查询缩略图,但是本人不建议使用。可以使用最新的ThumbnailUtils根据路径创建缩略图。

getImageThumbnail函数实现

static public Bitmap getImageThumbnail(String imagePath, int width, int height) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 获取这个图片的宽和高,注意此处的bitmap为null
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        options.inJustDecodeBounds = false; // 设为 false
        // 计算缩放比
        int h = options.outHeight;
        int w = options.outWidth;
        int beWidth = w / width;
        int beHeight = h / height;
        int be = 1;
        if (beWidth < beHeight) {
            be = beWidth;
        } else {
            be = beHeight;
        }
        if (be <= 0) {
            be = 1;
        }
        options.inSampleSize = be;
        // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }

最终实现功能如下:

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐