Picasso isn't loading scrolled images efficiently? - android

I am using a gridview that loads a bunch of images through picasso. I have implemented viewholders and I'm not sure what else I could do with the adapter or imageview to make it load better. I've isolated the issue to the image loading through an internet connection because the scrolling is fine when I load images locally.
The gridview scrolling is very buggy when using picasso and the scroll jumps around constantly when it shouldn't. What is the solution to this issue?

First of all I suggest you to use RecyclerView with GridLayoutManager or StaggeredGridLayoutManager. It must be more effective and view itself contain less bugs.
Next, use OkHttp with Picasso. It's more stable, powerful and consistent http client. And I think most important to you configure memory/file cache.
// Create a cache using an appropriate portion of the available RAM
Cache memoryCache = new LruCache(getApplicationContext());
// Use OkHttp as downloader
Downloader downloader = new OkHttpDownloader(getApplicationContext(),
PICASSO_DISK_CACHE_SIZE);
mPicasso = new Picasso.Builder(getApplicationContext())
.downloader(downloader).memoryCache(memoryCache).build();
Speed of loading also depend on network condition and image size.

Related

Displaying images in a Recyclerview

I have a recyclerview that I want to display images that I have saved in drawables in jpeg. For loading I was using an Async Task, but this doesn't preload or Cache them. I was told to use one of the tools like fresco or glide to do that, but which one can do the Job better?
The most important thing for me is that the app doesn't crash, but it would also be nice to be able to preload certain images already before the View is displayed and afterwards preload as many as memory allows.
You can use Fresco for this. An example for a recycler view that loads images from the network can be found here:
https://github.com/facebook/fresco/blob/master/samples/showcase/src/main/java/com/facebook/fresco/samples/showcase/drawee/DraweeRecyclerViewFragment.java

How to prevent load each row (image) in recycleview

I have to show an image into imageview in my layout for recycleview' s row.
RecycleView use ViewHolder pattern so my code is in custom ViewHolder.
To load the images I need to use an AsynkTask that return a File in onPostExecute(). In this onPostExecute() I setup imageView with Picasso.
I tried to obtain the same file without use AsynkTask. So I call directly Picaso.with()....
In this scenario, recycleview lag so much when scrolling, but i notified that Picasso cache all images.
The first scenario with AynkTask has more than one problem. Scrolling down and up for x times with 100 row make the queue of asynktask bigger.
Now, if I open a fragment of drawer, queue of Asynkask continues to load and this selected fragment make its work so slowly.
how can I fix it?
I would suggest you to checkout Image Management Library by Facebook that is Fresco that is pretty awesome and mature as compared to other Image Loading Library.
When image in a view goes out of screen it automatically recycles the bitmap, hence releasing the memory (Making scrolling smooth).
Fresco handles all the things caching of images with 3 Tier architecture ( BITMAP_MEMORY_CACHE, ENCODED_MEMORY_CACHE and DISK_CACHE).
It also reduces OOM (Out Of Memory) issues.

UIL, Picasso - Images in adapter always reload when stop scrolling

I have ListView with text and large image from internet. My image item has fit width and wrap_content height.
I tried to display image in background with UIL & Picasso. Both of them can work but the image always reloads when I stop scrolling, and it makes ListView flickering
It looks like this:
You can see that it reload downloaded and cached images when I stop scrolling (I scroll down and then scroll up).
How can I prevent this happen?
<ImageView android:id="#+id/imgFeed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
// UIL
options = new DisplayImageOptions.Builder()
.showImageOnLoading(defaultImage)
.showImageOnFail(defaultImage)
.showImageForEmptyUri(defaultImage)
.resetViewBeforeLoading(false)
.cacheOnDisk(true).delayBeforeLoading(0)
.displayer(new FadeInBitmapDisplayer(200)).cacheInMemory(true).imageScaleType(ImageScaleType.EXACTLY_STRETCHED).build();
ImageAware imageAware = new ImageViewAware(viewHolder.imgFeed, false);
ImageLoader.getInstance().displayImage(item.getPhotoUrl(), imageAware, options);
// Picasso
Picasso.with(getContext())
.load(item.getPhotoUrl())
.placeholder(R.drawable.place_holder_big)
.resize(screenWidth, 0) //set max width
.into(viewHolder.imgFeed);
For UIL, I tried many ways in this issue but they don't work for me at all.
Update: seems I faced with memory cache issue like this question. But how can I fix this issue? Look at Facebook app, they did it very well. All images have different sizes with fit width, and very smooth scrolling without reloading images. How can they do that?
If you're wondering how Facebook did it, they actually released their image loading library (https://github.com/facebook/fresco)
Is it possible you are actually calling notifyDataSetChanged on the underlying ListView at that time? Also are you using hasStableIds()?
For UIL you could try using a WeakMemoryCache (refer to https://github.com/nostra13/Android-Universal-Image-Loader/wiki/Useful-Info) as that'll theoretically allow you to make use of all available memory though it may cause a lot of extra GC calls.
For Picasso Taha's method looks like your best bet!
Maybe your memory cache size is small and Picasso tries to load images from disc cache. Please check here for deciding cache size. You can try to increase cache size of Picasso by:
Picasso p = new Picasso.Builder(context)
.memoryCache(new LruCache(cacheSize))
.build();
However in my opinion your app looks like having an endless feed. Which means your memory cache will be full at some time and you'll have to use disc cache. Retrieving data from the disc cache is slower compared to memory cache.

Grid View image load on scroll and image cache

I want to load the Async image loading for grid view. The image loading should be Async and should only when grid view scrolled. And when scroll up the image should be loaded from cache. Simply i need a loading style just like fb,pinterest.
Use Jake Wharton's Picasso Library. (A Perfect ImageLoading Library form the developer of ActionBarSherlock)
A powerful image downloading and caching library for Android.
Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Many common pitfalls of image loading on Android are handled automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter. Complex image transformations with minimal memory use. Automatic memory and disk caching.
Picasso Jake Wharton's Library
Your exact requirement in serviced by Android Universal Image Loader Library. Check out the github repository here
There is a sample code given here which shows the working of the loader for a grid layout.
Hopefully this will serve your need.

Which approach to use to download many images asynchronously

I've a ListView in which each row item contains many ImageViews apart from some content.
That is, there will be many list items, each containing many images.
In order to not to hold user unnecessarily, I'm showing the content first & then downloading the images asynchronously.
Currently, for each image I'm starting a new AsycTask.
Although, it working alright, but this not a good approach.
In place of this, what should I use?
use this library, best in android for image loading..
https://github.com/nostra13/Android-Universal-Image-Loader
Your scenario seems to fit exactly to use LazyLoading of images. This basically loads images in the background and shows them as soon as they're loaded.
You might want to see this:
Lazy load of images in ListView
An alternative would be using a ThreadPoolExecutor with the images to load, but the above works pretty well and is recommended.
Try using the library
http://square.github.io/picasso/
Many common pitfalls of image loading on Android are handled automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.

Categories

Resources