How do we give ImageCardViews placeholder images while the poster loads? - android

I have a wrapper class that contains the ImageCardView and loads an image fetched from the Web into it. Before the network call is made, I have
imageCardView.getMainImageView().setImageResource(R.drawable.icon_movie_placeholder);
For some reason, the image never comes up if I have the network call take place right afterwards, even though it takes maybe .5-1.5 seconds for an individual poster to come up. But if I comment out the network call and assignment, the image comes up. How do I make it so that the placeholder is visible while the poster is being fetched? I use Picasso to do this, does Picasso clear the image view before it loads?

I think Picasso does clear the image when you call the load(url).into(imageView) method in order to avoid displaying incorrect images on a listView/recyclerView.
What you could use is Picasso's own way of setting a placeholder image, which works like this
Picasso.with(context)
.load(imageUrl)
.placeholder(R.drawable.icon_movie_placeholder)
.into(imageView)
This way Picasso will show your icon_movie_placeholder drawable until the network request finishes, and if you need to provide a placeholder image you can use the builder's error(resourceId) method just like that.
You can find more info about this on Picasso's official Github page (specially under the Features section: http://square.github.io/picasso/

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)

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

Glide doesn't set GlideDrawable into ImageView after loading image

I'm loading images using Glide this way in a Fragment
Glide.with(this).load(pictureUrl)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.default)
.into(image);
but the image is never set into the ImageView although the image IS indeed being loaded (I investigated this using a listener). I believe this only happens when the image is not being loaded from memory since it does load if the Fragment is resumed.
If I add asBitmap() above, then the image successfully loads. Also, setting the GlideDrawable into the ImageView from an attached RequestListener's onResourceReady() method also works.
For anyone else with a similar problem, it turned out to be related to a custom View. See the post on the discussion list for more information.

Using volley to populate a fragment

I am working on a simple app and I am having issues understanding how to use volley for something I feel should be an easy task. Currently I have gotten volley to grab a json array that populates a list of images with titles next to them. The problem is I want to be able to press on an item in the list and spawn a new fragment with a larger version of the image in it. This seems like a really basic task, but I can't seem to find the right way to do it. If I grab the full images to create the list view, I run out of memory. I started grabbing thumbnail images, but then when I create the fragment, the view is inflated before the request for the image finishes. What is the right way to do this?
You can specify a placeholder image while Volley downloads your image, and an error image if the download wasn’t successful. In your situation, you can use your thumbnail image as the placeholder image.
The Making Image Request section in this artcle shows the good way to handle image request with Volley.
Sample code:
// Loading image with placeholder and error image
imageLoader.get(Const.URL_IMAGE, ImageLoader.getImageListener(
imageView, YOUR_THUNMBNAIL_IMAGE, R.drawable.ico_error));

Is it absolutely correct that Picasso understands NOT to load if the view has been recycled?

I'm a little confused: as a rule when async loading images to some sort of list view (whether on Android or iOS or in the abstract on another platform), you essentially must do this ..
-- make a note of "which" cell this is (say, #213)
-- start getting the image from the net.
-- it has loaded from the net. What cell are we now?
-- if we are "still" 213, load the image to the image view!
-- if we are "no longer" 213, just forget about it.
this is a basic in lazy-loading async images. For example, Lucas Rocha explains it perfectly in a famous article here:
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
(scroll down to exactly "Here is just a simplistic sketch of one way you could do it:" ...)
Well now, as I understand it Picasso in fact does this for you, completely automatically.
On its own, Picasso 'knows' if the view has changed. If the view has changed, Picasso knows not to bother loading it
Am I completely correct? This is a built-in feature of Picasso, and I need do nothing else?
(Aside - I'm somewhat confused "how" Picasso does this; glancing at it I can't see any magic code in Picasso where it makes a note of the id, or something of the holder? view? in question.)
Just to be clear, I'm using Picasso in the usual way exactly like this, essentially at the end of getView...
Picasso.
with(State.mainContext).
load(imageFile.getUrl()).
placeholder(R.drawable.default).
noFade().
into(v.hexaIV);
yes, Picasso is that beautiful. You only need that one line inside the getView() method Picasso.with(c).load(url).into(img);
how they exactly do it, I'm not sure, but before Picasso existed I did my own image loader and it's not so difficult.
Let's say you have a map of Url and ImageView somewhere in your image loader code.
So whenever the code pass img to it, it checks against this map, if it is already loading other URL for the same img using basic Java mImg.equals(img), if it matches, it knows that, even thou still will cache that URL, it should not deliver the Drawable to the ImageView.
There're a few rare cases that you might want to directly cancel a load, on those cases you can call Picasso.with(c).cancel(img);, but that's rare to be necessary.

Categories

Resources