Android Java recycle View scrolling: will it redownload images? - java

I am using picasso in my recycler view. I am wondering about the caching: When I scroll down the images disappear and when I scroll up again, they show again. But its displaying the progress bar for downloading and it takes like a second to show it.
my code
Picasso.get().load(profileUrl).placeholder(R.drawable.placeholder_image).fit().into(postViewHolder.profileImg);
I want to avoid downloading the same images multiple times. Or is it just the caching that takes some time to display the already cached image again?

Picasso should cache images automatically. You can can check if it's cached by using the setIndicatorsEnabled method on the Picasso singleton instance.
Red: Downloading over the network.
Blue: Image was cached on disc.
Green: Image was cached in memory.

Picasso by default cache image . if you dont want cache using this code
Picasso.with(context).load(uri).networkPolicy(NetworkPolicy.NO_CACHE).memoryPolicy(MemoryPlicy.NO_CACHE).placeholder(R.drawable.placeholder).into(imageView);

Related

Showing lists of image to RecyclerView from internet

I have a long list of images that i need to show in to recyclerView. I am using glide to show the images from my adapter.
Glide.with(context).load(feed.getImgLink()).diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.myImgView);
The above is the code am using i set the DiskCacheStrategy to none because I'm trying to show Instagram like Feed and i didn't want to permanently cache the img on disk.
The first time i see the image there is no problem it is displayed normal but after i scroll down and back up the image reload again. I get that i haven't cache the image in to disk but there is still momery cache.
Is there any way i can solve i could use diskcache to ALL but it is taking space for just one time viewing.
Try this.
Glide.with(context).load(imageUrl).into(holder.myImgView);

Android loading images to ListView AsyncTask

This is more a question of application logic and performance.
Project Setup: I have an Android ListView which inflates a layout that is more aesthetically pleasing than just simple text. In each row (7 to be exact), I have an Image that is fetched by URL.
The problem: This ListView and its data are set by an ArrayList of model objects that serve as temporary data holders. When the rows are added the model objects are then used to set the data in the specific row's UI. The issue then comes along, that when a user scrolls down on the list a row that was once viewable is now out of the user's view. When the user scrolls back up to that row, the whole process of fetching the Image then happens again.
This seems like it can be avoided by instead of passing URLs through the models then fetching the images, I should fetch the images first, then pass the bitmaps to the model, therefore when the row is visible again to the user, it does not have to re-load the image.
Is there a way to write an AsyncTask that can load 7 images successfully, or do I have to create an AsyncTask object for each image?
Everytime I go this route the application crashes...
You can use Picasso library for this task.
Visit http://square.github.io/picasso/
In the adapter of your ListView, in the method getView, you can put this:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_error)
.resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size) .centerInside()
.into(imgView);
You dont need a AsyncTask because this library already implements itself.

Universal-Image-Loader does not keep the displayed images

I am handling 3mb of images and the problem is by the time I release my finger from scrolling, the images, are somewhat downloaded from the cache in my case in the sd card. I am using PauseOnScrollListener, as well and it did not solve the problem. I wanted the images to remain on the grid even if I am not viewing it, while scrolling. Universal-Image-Loader handles it differently by not showing the images while not displayed, and by the time I pause and see a particular image then that's the time it gets displayed again.
options = new DisplayImageOptions.Builder()
.showStubImage(icon)
//Because you reuse view for different
//images you can see a previous image in the view while new image is loading. .resetViewBeforeLoading(true
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheOnDisc(true)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
If you are using a GridView, this is the normal behavior.
Actually what happens is that, when you scroll through, the elements that are not visible or which are scrolled out of the screen are destroyed or in other words nullified. The reason is memory optimization and this is done by android.
If you have any textview in your Grid Element, it will be loaded faster when it gets back into the screen. But for Images, the ImageLoader might take a little time to load the images from the cache into the ImageView. This is normal in my opinion.
If you don't want the images to be destroyed, then you will run into OutOfMemory exception for which this is much more better.

Modifying single list items

I have a ListView, displaying some items, containing an ImageView filled with a standard image at first and a line of text.
What I wanna do is downloading one thumb after another with an AsyncTask and step for step change the standard image with the downloaded one. How to realise this?
The ListView contents are managed by an enhanced ArrayAdapter.
greetz
EnflamedSoul
If I understand you correctly you're interested in a LazyList. This example shows a stub for the image, then downloads some thumbnails as the user scrolls down and caches these onto an SD card. The next time the images will be loaded from the SD card.
Lazy load of images in ListView
I personally used Fedor's example and it worked well.

preload webview

I have a webview and i have next previous buttons to get to the next or previous picture.
What i want to do if possible is : Can i display the first picture but the webview loads (in the background the other pictures(other URLS) so that are available in the cache when a new picture is needed. Thank you
This is possible if you use two Webviews and a ViewFlipper to change between them.
This way while the user looks at one, the other is loading the content. And when flips(you need to implement) it will bring up instantly the loaded image.

Categories