How to prevent of loading images from previous results? - android

I am implementing search and there are cases when images of old results are loaded and applyed to new ones, so how can I prevent Glide loading images and displaying on imageView when I have new search results.

If you're using Glide to load images, you can completely skip the caching of images by using something like below:
Glide.with(fragment)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(view);

Related

How to show Glide images when ready? They are shown incorrectly if not fully downloaded

I am working on an application that has this image gallery activity where images are loaded from disk using Glide. These images are downloaded separately through DownloadManager. The problem is that these images might not have been downloaded by the time the user opens the gallery. I guess one way would be to load these images dynamically once each file gets downloaded by going through a BroadcastReceiver but I am wondering if there is an easier solution? Some of the images only shows parts of itself while others don't show at all, and in order to view the full images I have to navigate back into the gallery so that Glide can redo the loading. I would instead like the images to be displayed once they are able to load. I also tried using a placeholder but it will never be replaced by the image (I would also like to avoid using a placeholder image since I don't have any). The images are displayed in a GridView and loaded in getView method of a BaseAdapter. (Currently I also have to avoid storing the images in cache otherwise the incomplete images will be shown the next time as well). Code:
val file = File(urls[position].toString())
Glide.with(context)
.asBitmap()
.load(file)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.fitCenter()
.apply(RequestOptions())
.into(imageView)

Out of Memory issue in a gallery like application

I am trying to create an application which contains lots of Images from web services.I am using Picasso library for loading images into a gallery like fragment with RecyclerView having lazy load and passing list of URL's to slider activity with ViewPager i am dealing lot of images so after few slides it starts throwing Out of Memory(OOM) exception.I have tried with Glide its is stretching the image so i stick with Picasso.Tried a lot of methods like using large heap and allowing hardware access.Any method to identify and handle this issue would be helpful.
Picasso.with(context)
.load(image.getImageURL())
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.skipMemoryCache()
.placeholder(R.drawable.poster_default)
.error(R.drawable.poster_default)
.into(mImageView);
Agree with previous answer. You can also try use RGB_565 config. The image with this config two times smaller
Picasso picasso = new Picasso.Builder(this)
.defaultBitmapConfig(Bitmap.Config.RGB_565)
.build();
And set this instance to picasso
Picasso.setSingletonInstance(picasso);
The code snippet should be placed in onCreate method in android.app.Application class
Android uses bitmaps for images so that would mean adding large images will consume tons of memory. What I do in my projects is add fit() to the Picasso call to it so it will resize the image for me instead of loading it full size in memory.
Try something like below and see if it's fixed
Picasso.with(context)
.load(image.getImageURL())
.fit()
.centerCrop()
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.skipMemoryCache()
.placeholder(R.drawable.poster_default)
.error(R.drawable.poster_default)
.into(mImageView);

How to check is Image is already cached in android mobile using Glide

I am storing my image and its reduced size image (blurred) in my Amazon Server, and store both path in database.
Now I want to know how to show blurred image first if original image is not cached and on clicking download it will download original Image. I am using Glide here...
I tried this
Glide.with(this)
.load(mainUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.thumbnail(Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE))
.centerCrop()
.into(imageView);
but problem is It automatically download original image in background.
I asked in Glide Github.
https://github.com/bumptech/glide/issues/2051
So add apply(RequestOptions.onlyRetrieveFromCache()) to your RequestOptions. You can register a listener and onFailed gets called when no image is in the cache.
Glide.with(TheActivity.this)
.load("http://sampleurl.com/sample.gif")
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(theImageView);
Your code will prevent Glide from downloading the GIF and will only show the GIF if it is already cached, which it sounds like you don't want.
Yes, the old image will eventually be removed. By default Glide uses an LRU cache, so when the cache is full, the least recently used image will be removed. You can easily customize the size of the cache to help this along if you want. See the Configuration wiki page for how to change the cache size.
Unfortunately there isn't any way to influence the contents of the cache directly. You cannot either remove an item explicitly, or force one to be kept. In practice with an appropriate disk cache size you usually don't need to worry about doing either. If you display your image often enough, it won't be evicted. If you try to cache additional items and run out of space in the cache, older items will be evicted automatically to make space.
If you want to show placeholder before image is loading then use code below:-
Glide.with(this)
.load(mainUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder("provide placeholder image here")
.thumbnail(Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE))
.centerCrop()
.into(imageView);

Loading Same Gallery Image Into Two Different Activity Fragments Android Picasso

Currently I have a Main Activity which has a Fragment loaded into it with a RecyclerView. In the RecyclerView adapter I use Picasso to load in images from the user's gallery based on a stored URI string I have like so:
Picasso.with(context)
.load(imageUri)
.resize(400, 400)
.into(viewHolder.imageView);
When the user clicks on one of these items in the RecyclerView I go to a new Activity and inside its own Fragment onCreateView I load the same image again but size it slightly larger (as the ImageView I'm loading it into is larger size)
Picasso.with(getActivity())
.load(imageUri)
.resize(600, 600)
.centerCrop()
.into(imageView);
My question is...Assuming these images can be quite large...
Is there a better way to load in this image once at say 600px X 600px back when the RecyclerView first needs them, and then reuse the bitmap data in the subsequent Activities and Fragments which need it without reloading again?
I'm open to using a different image loader like Volley if that's better.
Any help would be appreciated.
Marco
You could try Glide it has almost the same api. Instead of Picasso glide has own cache implementation, when Picasso just use http cache and it's hard to control. In glide you need to specify diskCacheStrategy.ALL to make glide cache all you want. Here is cache wiki

Reducing Image Size at Runtime for Android app

I am parsing a json schema which contains textual info and image urls for my android app. I want to read all the images from the schema and then show them in the gallery view of my android app.
But the problem is, the image urls contain HD pics and it takes a lot of time to load. Is there a way I can reduce the size of those images at run time and then display or can you suggest any improvement tip so that the images could load quickly from the schema?
Thanks
Try using Picasso library, it can resize your images whatever you like and bind it to ImageView. See also my answer here.
But if your images on your server are too big, it all depends on your internet connection, how fast they get to your device.
You can resize your image downloaded from url using picasso library. Also it will allow lazy loading of images and you can also set placeholder and error images in your imageview.
You can read complete documentation here: http://square.github.io/picasso/
Picasso.with(context)
.load(url)
.resize(50, 50)// resizing images
.centerCrop()
.into(imageView)

Categories

Resources