第二十一篇 Android listview异步加载本地视频缩略图

最近项目中需要在listview上加载视频缩略图,如果直接使用ThumbnailUtils类获取会出现oom,然后参考了网上这边

文章,感觉不错,稍微做了下调整,这里做个记录,方便后续复用.

MyVideoThumbLoader类:

public class MyVideoThumbLoader {

// 创建cache
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, MyImageView imgview) {

    if (getVideoThumbToCache(path) == null) {
        // 异步加载
        new MyBobAsynctack(imgview, path).execute(path);
    } else {
        imgview.setImageBitmap(getVideoThumbToCache(path));
    }

}

class MyBobAsynctack extends AsyncTask<String, Void, Bitmap> {
    private MyImageView imgView;
    private String path;

    public MyBobAsynctack(MyImageView imageView, String path) {
        this.imgView = imageView;
        this.path = path;
    }

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

        Bitmap bitmap2 = null;

        try {

            ThumbnailUtils tu = new ThumbnailUtils();

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

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

            if (bitmap == null) {

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

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

            // //直接对Bitmap进行缩略操作,最后一个参数定义为OPTIONS_RECYCLE_INPUT ,来回收资源

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

            // 加入缓存中
            if (getVideoThumbToCache(params[0]) == null) {
                addVideoThumbToCache(path, bitmap2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmap2;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imgView.getTag().equals(path)) {// 通过 Tag可以绑定 图片地址和
            // imageView,这是解决Listview加载图片错位的解决办法之一
            imgView.setImageBitmap(bitmap);
        }
    }
}
}

MyImageView类:需要使用MyImageView,不能使用ImageView来显示视频缩略图.

public class MyImageView extends ImageView {

public MyImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyImageView(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    try {
        super.onDraw(canvas);
    } catch (Exception e) {
        System.out.println("trying to use a recycled bitmap");
    }
}

}

VideoAdapter类:可根据业务需求自定义.

public class VideoAdapter extends BaseAdapter {

List<FileBean> datas;
private LayoutInflater mInflater;
private int resources;
private Context mContext;
private MyVideoThumbLoader mVideoThumbLoader;

public VideoAdapter(Context mContext, int resources, List<FileBean> datas) {
    this.mContext = mContext;
    this.resources = resources;
    this.datas = datas;
    mInflater = LayoutInflater.from(mContext);
    mVideoThumbLoader = new MyVideoThumbLoader(mContext);// 初始化缩略图载入方法
}

@Override
public int getCount() {
    if (datas==null)return 0;
    return datas.size();
}

@Override
public Object getItem(int position) {
    if (position < 0 || position >= datas.size())
        return null;
    return datas.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    FileBean entity = datas.get(position);
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(resources, parent, false);
        holder.fileIcon = (MyImageView) convertView.findViewById(R.id.image);
        holder.fileName = (TextView) convertView.findViewById(R.id.name);
        holder.time = (TextView) convertView.findViewById(R.id.time);
        holder.fileSize = (TextView) convertView.findViewById(R.id.size);
        holder.check = (ImageView) convertView.findViewById(R.id.ivCheck);
        convertView.setTag(holder);
        holder.fileIcon.setTag(entity.getFile().getAbsolutePath());
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (entity.getFile().length() <= 0) {
        holder.fileIcon.setImageResource(R.drawable.icon_video);
    }else{
        mVideoThumbLoader.showThumbByAsynctack(entity.getFile().getAbsolutePath(), holder.fileIcon);
    }


    holder.fileName.setText(entity.getFileName());
    holder.time.setText(String.valueOf(entity.getFile().lastModified()));
    holder.fileSize.setText(FileUtils.formatSize(String.valueOf(entity.getFileSize())));
    holder.check.setVisibility(entity.isChoose() ? View.VISIBLE : View.GONE);

    return convertView;
}

class ViewHolder {
    MyImageView fileIcon;
    TextView fileName;
    TextView time;
    TextView fileSize;
    ImageView check;
}
}

猜你喜欢

转载自blog.csdn.net/hhy113835/article/details/78996987