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);
}
}
}
}
原生方法异步加载视频缩略图(笔记)
最新推荐文章于 2021-05-31 15:16:54 发布