Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

android - Image thumbnails not setting correctly

I am trying to make a file explorer. I achieved it all except that my image thumbnails were loading too slow. So I read that I must move to asynctask and then in post execute update the ui. Before running asynctask I provide a default thumbnail to all my files. In asynctask I take position as parameter because I set tags using them. Then I check the file using that position. If thats image or video I create a bitmap for it. In post execute I create a new viewholder and assign it the same tag using getTag(position). Then I assign the bitmap using setBitmapImage. Problem: Thumbnails are changing/blinking even for the files that are not images/videos. Things I have tried so far: a)updating of Image Thumbnails using AsyncTask for Android ListView not coming proper
b)Android ListView updating of Image Thumbnails using AsyncTask Causes View recycling

here is my code for getView:

@Override
public View getView(final int position,  View convertView, ViewGroup parent) {


    File file=new File(String.valueOf(filenames[position]));
    if(file.isDirectory()){
        img=R.drawable.folder;
    }

    else{
        img=getImage(filenames[position]);
    }

    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = LayoutInflater.from(getContext());
        convertView = inflator.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
        viewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
        viewHolder.iv= (ImageView)convertView.findViewById(R.id.imageView);
        viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                myList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
            }
        });
        convertView.setTag(viewHolder);
        convertView.setTag(R.id.textView1, viewHolder.tv);
        convertView.setTag(R.id.checkBox1, viewHolder.cb);
        convertView.setTag(R.id.checkBox1, viewHolder.iv);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.iv.getLayoutParams().height = 80;
    viewHolder.iv.getLayoutParams().width = 80;
    viewHolder.cb.setTag(position);
    final ViewHolder ViewHolder= viewHolder;
    ViewHolder.iv.setImageResource(img);
    ViewHolder.tv.setText(myList.get(position).getName());
    ViewHolder.cb.setChecked(myList.get(position).isSelected());


    final View finalConvertView = convertView;
    class myTask extends AsyncTask<Integer,Void,Bitmap>{

        int pos;
        public myTask(int pos){
            this.pos=pos;
        }

        @Override
        protected Bitmap doInBackground(Integer... params) {
            Bitmap thumb=null;

            if((String.valueOf(filenames[pos])).contains(".jpeg")||String.valueOf(filenames[pos]).contains(".jpg")||String.valueOf(filenames[pos]).contains(".png")||String.valueOf(filenames[pos]).contains(".bmp")||String.valueOf(filenames[pos]).contains(".webp")||String.valueOf(filenames[pos]).contains(".gif")){
                thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(String.valueOf(filenames[pos])), 64, 64);

            }
            else if((String.valueOf(filenames[pos])).contains(".mp4")||String.valueOf(filenames[pos]).contains(".mkv")||String.valueOf(filenames[pos]).contains(".webm")||String.valueOf(filenames[pos]).contains(".3gp")||String.valueOf(filenames[pos]).contains(".3gpp")){
                thumb=ThumbnailUtils.createVideoThumbnail(filenames[pos].getPath(), MediaStore.Video.Thumbnails.MINI_KIND);

            }
            return thumb;
        }
        @Override
        protected void onPostExecute(Bitmap thumb){
            if(isCancelled()){
                return;
            }
            ViewHolder v= (ViewHolder) finalConvertView.getTag(pos);

            if((String.valueOf(filenames[pos])).contains(".jpeg")||String.valueOf(filenames[pos]).contains(".jpg")||String.valueOf(filenames[pos]).contains(".png")||String.valueOf(filenames[pos]).contains(".bmp")||String.valueOf(filenames[pos]).contains(".webp")||String.valueOf(filenames[pos]).contains(".gif")){

                v.iv.setImageBitmap(thumb);
            }
            else if((String.valueOf(filenames[pos])).contains(".mp4")||String.valueOf(filenames[pos]).contains(".mkv")||String.valueOf(filenames[pos]).contains(".webm")||String.valueOf(filenames[pos]).contains(".3gp")||String.valueOf(filenames[pos]).contains(".3gpp")){

                v.iv.setImageBitmap(thumb);
            }


        }
    }

    myTask m=new myTask(position);
    m.execute();

   return convertView;



}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have finally found answer to this.

Here is the ListAdapter Class:

class ListAdapter extends ArrayAdapter<Modal> {

    private final Context context;
    private int img;

    private List<Modal> myList;
    private File filenames[];
    private List<Integer> selectedItems=new ArrayList<Integer>();
    private CheckBox cb;
    public ListAdapter(Context context, List<Modal> list, File filenames[]) {
        super(context, R.layout.list_item, list);
        this.context = context;
        this.myList = list;
        this.filenames=filenames;
    }


    @Override
    public int getViewTypeCount() {

        if(getCount()>0){
            return getCount();
        }
        return 1;
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

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


    static class ViewHolder {
        TextView tv;
        CheckBox cb;
        static ImageView iv;
    }

    public int getImage(File filename){
        if(String.valueOf(filename).contains(".rar")||String.valueOf(filename).contains(".zip")){
            return R.drawable.archive;
        }
        else if(String.valueOf(filename).contains(".doc")||String.valueOf(filename).contains(".docx")){
            return  R.drawable.doc;
        }
        else if(String.valueOf(filename).contains(".pdf")){
            return R.drawable.pdf;
        }
        else if(String.valueOf(filename).contains(".txt")){
            return R.drawable.txt;
        }
        else if(String.valueOf(filename).contains(".ppt")||String.valueOf(filename).contains(".pptx")){
            return R.drawable.ppt;
        }
        else if(String.valueOf(filename).contains(".xls")||String.valueOf(filename).contains(".xlsx")){
            return R.drawable.xls;
        }
        else if((String.valueOf(filename).contains(".jpeg")||String.valueOf(filename).contains(".jpg")||String.valueOf(filename).contains(".png")||String.valueOf(filename).contains(".bmp")||String.valueOf(filename).contains(".webp")||String.valueOf(filename).contains(".gif"))){
            return R.drawable.image;
        }
        else if((String.valueOf(filename).contains(".mp4")||String.valueOf(filename).contains(".mkv")||String.valueOf(filename).contains(".webm")||String.valueOf(filename).contains(".3gp")||String.valueOf(filename).contains(".3gpp"))||String.valueOf(filename).contains(".MOV")){
            return R.drawable.video;
        }
        else if((String.valueOf(filename).contains(".mp3")||String.valueOf(filename).contains(".wav")||String.valueOf(filename).contains(".wma")||String.valueOf(filename).contains(".MP2")||String.valueOf(filename).contains(".AAC")||String.valueOf(filename).contains(".AC3")||String.valueOf(filename).contains(".AU")||String.valueOf(filename).contains(".OGG")||String.valueOf(filename).contains(".FLAC"))){
            return R.drawable.music;
        }

        else {
            return R.drawable.unknown;
        }

    }


    @Override
    public View getView(final int position,  View convertView, ViewGroup parent) {




        File file=new File(String.valueOf(filenames[position]));
        if(file.isDirectory()){
            img=R.drawable.folder;
        }

        else{
            img=getImage(filenames[position]);
        }


        ViewHolder viewHolder = null;

        if (convertView == null) {
            LayoutInflater inflator = LayoutInflater.from(getContext());
            convertView = inflator.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
            viewHolder.iv= (ImageView)convertView.findViewById(imageView);
            viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    myList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                }
            });
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.textView1, viewHolder.tv);
            convertView.setTag(R.id.checkBox1, viewHolder.cb);
            convertView.setTag(R.id.checkBox1, viewHolder.iv);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.iv.getLayoutParams().height = 150;
        viewHolder.iv.getLayoutParams().width = 150;
        viewHolder.cb.setTag(position);
        final ViewHolder ViewHolder= viewHolder;

        ViewHolder.tv.setText(myList.get(position).getName());
        ViewHolder.cb.setChecked(myList.get(position).isSelected());
        viewHolder.iv.setImageResource(img);


        Log.v("this","viewHolderMT: "+String.valueOf(viewHolder)+" position: "+position);
        myTask m=new myTask(position,viewHolder.iv);
        m.execute();

       return convertView;



    }
    class myTask extends AsyncTask<Integer,Bitmap,Bitmap>{



        int pos;
        ImageView iv;
        public myTask(int pos,ImageView iv){
            this.pos=pos;
            this.iv=iv;
            iv.setTag(pos);


        }

        @Override
        protected Bitmap doInBackground(Integer... params) {
            Bitmap thumb=null;


            if((String.valueOf(filenames[pos])).contains(".jpeg")||String.valueOf(filenames[pos]).contains(".jpg")||String.valueOf(filenames[pos]).contains(".png")||String.valueOf(filenames[pos]).contains(".bmp")||String.valueOf(filenames[pos]).contains(".webp")||String.valueOf(filenames[pos]).contains(".gif")){

                thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(String.valueOf(filenames[pos])), 150, 150);

            }
            else if((String.valueOf(filenames[pos])).contains(".mp4")||String.valueOf(filenames[pos]).contains(".mkv")||String.valueOf(filenames[pos]).contains(".webm")||String.valueOf(filenames[pos]).contains(".3gp")||String.valueOf(filenames[pos]).contains(".3gpp")||String.valueOf(filenames[pos]).contains(".MOV")){
                thumb=ThumbnailUtils.createVideoThumbnail(filenames[pos].getPath(), MediaStore.Video.Thumbnails.MINI_KIND);


            }
            return thumb;
        }
        @Override
        protected void onProgressUpdate(Bitmap... thumb){


        }
        @Override
        protected void onPostExecute(Bitmap thumb){


            if(thumb!=null && (Integer)iv.getTag()==this.pos) {
                if ((String.valueOf(filenames[pos])).contains(".jpeg") || String.valueOf(filenames[pos]).contains(".jpg") || String.valueOf(filenames[pos]).contains(".png") || String.valueOf(filenames[pos]).contains(".bmp") || String.valueOf(filenames[pos]).contains(".webp") || String.valueOf(filenames[pos]).contains(".gif")) {

                    iv.setImageBitmap(thumb);

                }
                 else if ((String.valueOf(filenames[pos])).contains(".mp4") ||String.valueOf(filenames[pos]).contains(".MOV")|| String.valueOf(filenames[pos]).contains(".mkv") || String.valueOf(filenames[pos]).contains(".webm") || String.valueOf(filenames[pos]).contains(".3gp") || String.valueOf(filenames[pos]).contains(".3gpp")) {

                    iv.setImageBitmap(thumb);
                }


            }





        }
    }
}

Mistakes I was making:

  • Move AsyncTask outside getview()
  • Give a reference to the imageView inside the async constructor
  • Also Implement the getItemViewType() and getItemId() functions
  • Was not checking the position tag in onPostExecute so the images were shuffling.

This took a while for me to figure out. Hope it helps others!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...