原生方法异步加载视频缩略图(笔记)

public class MyVideoThumbLoader {

    private LruCache<String, Bitmap> lruCache;
    private Context mContext;

    public MyVideoThumbLoader(Context context) {
        mContext = context;
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int maxSize = maxMemory / 4;
        lruCache = new LruCache<String, Bitmap>(maxSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getByteCount();
            }
        };
    }

    public void addVideoThumbToCache(String path, Bitmap bitmap) {
        if (getVideoThumbToCache(path) == null) {

            lruCache.put(path, bitmap);
        }
    }

    public Bitmap getVideoThumbToCache(String path) {

        return lruCache.get(path);

    }

    public void showThumbByAsyncTack(String path, CustomRoundAngleImageView imgview) {

        if (getVideoThumbToCache(path) == null) {
            new ShiroAsyncTack(imgview, path).execute(path);
        } else {
            imgview.setImageBitmap(getVideoThumbToCache(path));
        }

    }

    class ShiroAsyncTack extends AsyncTask<String, Void, Bitmap> {
        private CustomRoundAngleImageView imgView;
        private String path;

        public ShiroAsyncTack(CustomRoundAngleImageView imageView, String path) {
            this.imgView = imageView;
            this.path = path;
        }

        @Override
        protected Bitmap doInBackground(String... params) {

//            Bitmap bitmap2 = null;
            Bitmap bitmap = null;
            try {

                ThumbnailUtils tu = new ThumbnailUtils();

                 bitmap = tu.createVideoThumbnail(params[0],
                        MediaStore.Video.Thumbnails.MINI_KIND);

                System.out.println("111111path: " + path + "  bitmap: "
                        + bitmap);

                if (bitmap == null) {

                    //将drawable转bitmap
                    bitmap = android.graphics.BitmapFactory.decodeResource(
                            mContext.getResources(),
                            R.drawable.album_video_default_cover);

                    System.out.println("5555555path: " + path + "  bitmap: "
                            + bitmap);
                }

//                bitmap2 = tu.extractThumbnail(bitmap, 180, 180,
//                        ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
//                System.out.println("path: " + path + "bitmap2: " + bitmap2);

                if (getVideoThumbToCache(params[0]) == null) {
                    addVideoThumbToCache(path, bitmap);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (imgView.getTag().equals(path)) {
                imgView.setImageBitmap(bitmap);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值