在ListView中重复图像...从视频中加载缩略图

时间:2016-02-22 20:11:02

标签: android listview bitmap mediastore

我正在尝试从MediaStore加载视频缩略图但是由于列表视图的转换/回收视图行为图像正在重复...滚动时在不同位置的相同图像

这是我的适配器代码

public class VideoListAdapter extends BaseAdapter {

    private ArrayList<VideoDetails> mVideosList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    ContentResolver crThumb;
    private float mSize;

    BitmapFactory.Options options=new BitmapFactory.Options();


    public VideoListAdapter(ArrayList<VideoDetails> mVideosList, Context mContext) {
        this.mVideosList = mVideosList;
        this.mContext = mContext;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        options.inSampleSize = 1;
        crThumb = mContext.getContentResolver();
    }

    @Override
    public int getCount() {
        return mVideosList.size();
    }

    @Override
    public Object getItem(int position) {
        return mVideosList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.video_row_layout,null,false);
            holder.mVideoTitleTextView = (TextView) convertView.findViewById(R.id.VideoTitleTextView);
            holder.mVideoSizeTextView = (TextView) convertView.findViewById(R.id.videoSizeTextView);
            holder.mVideoThumbImage = (ImageView) convertView.findViewById(R.id.videoThumbImageView);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mVideoTitleTextView.setText(mVideosList.get(position).getTitle());
        mSize = Float.parseFloat(mVideosList.get(position).getVideoSize());
        holder.mVideoSizeTextView.setText(" Size(MB):"+mSize/1024*1024);
        Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, mVideosList.get(position).getVideoId(), MediaStore.Video.Thumbnails.MICRO_KIND, options);
        holder.mVideoThumbImage.setImageBitmap(curThumb);
        return  convertView;
    }

    private static class ViewHolder
    {
        TextView mVideoTitleTextView;
        TextView mVideoSizeTextView;
        ImageView mVideoThumbImage;
    }
}

无论如何我可以解决这个问题...... ???? 早期从服务器下载图像我通过保持Url,异步Drawable和异步任务的周引用解决了这个问题但是从存储加载时如何解决此问题

1 个答案:

答案 0 :(得分:2)

请试试这个

像这样更改getView方法。

public View getView(int position, View convertView, ViewGroup parent) {
    view=convertView;
    ViewHolder holder;
    if(convertView == null)
    {
        holder = new ViewHolder();
        view = mLayoutInflater.inflate(R.layout.video_row_layout,null,false);
        holder.mVideoTitleTextView = (TextView) convertView.findViewById(R.id.VideoTitleTextView);
        holder.mVideoSizeTextView = (TextView) convertView.findViewById(R.id.videoSizeTextView);
        holder.mVideoThumbImage = (ImageView) convertView.findViewById(R.id.videoThumbImageView);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.mVideoTitleTextView.setText(mVideosList.get(position).getTitle());
    mSize = Float.parseFloat(mVideosList.get(position).getVideoSize());
    holder.mVideoSizeTextView.setText(" Size(MB):"+mSize/1024*1024);
    Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, mVideosList.get(position).getVideoId(), MediaStore.Video.Thumbnails.MICRO_KIND, options);
    holder.mVideoThumbImage.setImageBitmap(curThumb);
    return  view;
}

并声明View view = null;作为班级变量。 希望能帮助到你。谢谢。请参考这里。 https://androidruler.wordpress.com/2016/02/21/android-custom-listview-example/