Android图片加载(2)

缩略图

上一篇博客我们讲述了怎么查找所有图片信息,其中包括构造实体对象以及使用Cursor,ContentResolver,MediaProvider类进行数据查找,还没有看过的小伙伴可以点击这里哦上一讲

接下来我们要干的工作就是拿到视频和照片的缩略图,吼吼,这里面回用到ThumbnailUtils和MediaMetadataRetriever类。

图片缩略图的获取
由于用到的类和代码都比较简单,我直接贴出代码,有疑问可以留言问我:

public Bitmap getImageThumbnail(int imageId,String imagePath, int width, int height, boolean isFull) {
//为了方便调用,我传进来很多项,大家可以根据自己的项目需要进行精简
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        if (bitmap == null ) {
            options.inJustDecodeBounds = true;
            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 = BitmapFactory.decodeFile(imagePath, options);
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        if (bitmap == null) {
            return null;
        }
        return bitmap;
    }

另外在进行图片压缩的时候我们还可以采用其他策略,在此就不一一列举,后面有机会做一个集合贴。

视频缩略图的获取
同上的原因,直接扔代码:

public Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind,boolean isFull) {
        Bitmap bitmap = null;
            bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        if (bitmap == null){
            return null;
        }
        return bitmap;
    }

    public Bitmap getVideoThumbnail1(String videoPath,int width, int height,boolean isFull){
        Bitmap bitmap = null;
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            try {
                retriever.setDataSource(videoPath);
                bitmap = retriever.getFrameAtTime(1000);
            } catch (IllegalArgumentException ex) {
            } catch (RuntimeException ex) {
            } finally {
                try {
                    retriever.release();
                } catch (RuntimeException ex) {
                    // Ignore failures while cleaning up.
                }
            }
            if (bitmap == null) return null;
            int mwidth = bitmap.getWidth();
            int mheight = bitmap.getHeight();
            if (mwidth > width) {
                float scale = (float) width / mwidth;
                float scale2=(float) height/mheight;
                int w = Math.round(scale * mwidth);
                int h = Math.round(scale2 * mheight);
                bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
            }
        if (bitmap == null){
            return null;
        }
        return bitmap;
    }

对于视频,我给了两个获取缩略图的方法,大家可以参考使用。
另外我们还可以通过MediaStore.Images.Thumbnail或者MediaStore.Videos.Thumbnail以及ContentProvider等方式获得缩略图,有兴趣的小伙伴可以自己去百度一下,我就不逐一罗列了

你可能感兴趣的:(android,图片)