Skip to content Skip to sidebar Skip to footer

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 updat

Solution 1:

I have finally found answer to this.

Here is the ListAdapter Class:

classListAdapterextendsArrayAdapter<Modal> {

    privatefinal Context context;
    privateint img;

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


    @OverridepublicintgetViewTypeCount() {

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

    @OverridepublicintgetItemViewType(int position) {

        return position;
    }

    @OverridepubliclonggetItemId(int position) {
        return position;
    }


    staticclassViewHolder {
        TextView tv;
        CheckBox cb;
        static ImageView iv;
    }

    publicintgetImage(File filename){
        if(String.valueOf(filename).contains(".rar")||String.valueOf(filename).contains(".zip")){
            return R.drawable.archive;
        }
        elseif(String.valueOf(filename).contains(".doc")||String.valueOf(filename).contains(".docx")){
            return  R.drawable.doc;
        }
        elseif(String.valueOf(filename).contains(".pdf")){
            return R.drawable.pdf;
        }
        elseif(String.valueOf(filename).contains(".txt")){
            return R.drawable.txt;
        }
        elseif(String.valueOf(filename).contains(".ppt")||String.valueOf(filename).contains(".pptx")){
            return R.drawable.ppt;
        }
        elseif(String.valueOf(filename).contains(".xls")||String.valueOf(filename).contains(".xlsx")){
            return R.drawable.xls;
        }
        elseif((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;
        }
        elseif((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;
        }
        elseif((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;
        }

    }


    @Overridepublic View getView(finalint position,  View convertView, ViewGroup parent) {




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

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


        ViewHolderviewHolder=null;

        if (convertView == null) {
            LayoutInflaterinflator= LayoutInflater.from(getContext());
            convertView = inflator.inflate(R.layout.list_item, null);
            viewHolder = newViewHolder();
            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(newCompoundButton.OnCheckedChangeListener() {

                @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    intgetPosition= (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=newmyTask(position,viewHolder.iv);
        m.execute();

       return convertView;



    }
    classmyTaskextendsAsyncTask<Integer,Bitmap,Bitmap>{



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


        }

        @Overrideprotected 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);

            }
            elseif((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;
        }
        @OverrideprotectedvoidonProgressUpdate(Bitmap... thumb){


        }
        @OverrideprotectedvoidonPostExecute(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);

                }
                 elseif ((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!

Post a Comment for "Image Thumbnails Not Setting Correctly"