androidandroid-glideandroid-assetsbitmapdrawablediskcache

Duplicate images while loading the images from android assets as Bitmap and setting ImaegView using glideV4


I tried to retrieve bitmap from the assets and then try to load the Horizontal recyclerview image list using glide as asBitmap(). I am getting the duplicate images and the mismatch images(loading wrong image in place of the required image). Below screen shot shows laptop and Mobiles have same bitmap loaded and Footware and Bus has wrong bitmap loaded Here laptop and Mobiles have same bitmap loaded and Footware and Bus has wrong bitmap loaded It worked well after I removed the glide cache using .skipMemoryCache(true).

I want to resolve the same want to use the glide Cache to load the images.

Tried following points which was suggested in this page: Glide recyclerview loading duplicate image 1)I have added palceholder. 2) clearing the bitmap--> Glide.with(context).clear(holder.imgcat); or using holder.imgcat.setImageBitmap(null); 3) skipMemoryCache(true) worked well but require cache for loading images.

InputStream selectedInputStream = Application.get().getAssets().open(ImageSaver.selectedAssets + "/" + mValues.get(position).getImage_url());
                Drawable selectedImagesDrawable = Drawable.createFromStream(selectedInputStream, null);
                Bitmap selectedicon = ((BitmapDrawable) selectedImagesDrawable).getBitmap();
                if(selectedInputStream != null) {
                    try {
                        selectedInputStream.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                GlideApp.with(context)
                        .asBitmap()
                        .load(selectedicon)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .error(R.drawable.placeholder)
                        .placeholder(R.drawable.placeholder)
                        .into(holder.imgcat);

I am getting the duplicate images and the mismatch images(loading wrong image in place of the required image) while set .diskCacheStrategy(DiskCacheStrategy.RESOURCE) or .diskCacheStrategy(DiskCacheStrategy.ALL).

skipMemoryCache(true) worked well but require cache for loading images


Solution

  • this problem sometimes happens in recyclerviews recyclerviews use the same ViewHolders to show their list items . so chances are that more than one request is being called on one ImageView of your viewholders at the same time.a fix for this could be to stop any other Glide requests already happenning for that ImageView.

    //in your onBindViewHolder 
    fun onBindViewHolder( holder:ViewHolder ,position:Int){
       //call this to clear previous requests
       Glide.with(context).clear(holder.imageView)
       //then make new request
       Glide.with(context).load(items[position].url).into(holder.imageview)
       //other codes
    }