Displaying images in a Recyclerview - android

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

Related

RecyclerView stutters when scrolling only 14 images

I've been trying to create a Recycler View full of Card Views. Currently, I'm using the Recycler View from this tutorial and loading it with 14 different images. The professional quality images range in size from 134K to 242K.(Down from 8MB - 18MB)
I know that the images may be too big but, I feel there must be some way to get better performance while scrolling. Can someone point me in the right direction?
Edit: The images will be stored on the device. There will probably never be more than 20 of them.
You can use Picasso library or Android Universal Image Loader
Picasso
Android-Universal-Image-Loader
You don't need any AsyncTask.Theese 2 library handling image operations in background so you can keep scrolling smoothly. I am using Picasso in my project right now. You can add error drawable , temporary placeholder default drawable , and its so simple automatically caching.
Just use one of them in onBindViewHolder to bind any image to imageView
Load the images in a separate thread (or AsyncTask).

Download image as thumbnail in listview adapter android

I have a decent android app which is using two fragments one of them is Listfragment. And my question is, can I use Asynctask to download the image and compress it to a thumbnail and then assign it to an imageview ? (Imagine there are like 100 items in ListView). Or is this even possible to call the asynctask each time ?
You don't need to reinvent the wheel. There are libraries which already can download, cache and resize image for you. For instance Picasso:
Picasso
.with(context)
.load(url)
.resizeDimen(width, height)
.centerCrop()
.error(R.drawable.error_img);
.into(placeholder);
For this task, I would strongly recommend the use of an external library.
There are many to choose from that can be used to download, display and cache images from an online source. As many of these libraries have been developed for many years, they are usually going to be better than any bespoke code you write.

Asynchronous bitmap buffering with GridView in Android

I am bulding up a grid of images for an app I'm building. It works like so:
Build up a list of Image IDs, which I must query using a different content provider each (these are images from MMS threads)
Create new activity, which hosts an ImageGridFragment. This fragment has a custom adapter that takes the grid of images, and loads each one as a bitmap asynchronously.
After images are loaded, they are cached in an LRU cache so I don't need to run unnecessary computation
So far, everything works quite well. However, I would like to pre-buffer images so that when the user scrolls down, s/he doesn't have to wait for images to load. They should already be loaded. The stock Android Gallery accomplishes. I've had a look at the source, but think there must be a more straightforward way.
To answer members' questions
Images are loaded one by one using the content://mms/part/xxx, where xxx is the ID of an image. These are MMS images, and to my knowledge, cannot be loaded as a batch process (though, maybe I'm wrong). I use a content provider in an AsyncTask to load each image
I've tried the following:
Pre buffer 30 images or so right when the fragment is created. This is not ideal because the massive I/O request, actually prevents the on-screen images from loading quickly (but the buffering does work well!)
Detect when the requested view to load is at the very bottom-right hand corner of the screen, which could work, but then would fail in the case that the GridView takes up only part of the screen. It also seems like there should be a cleaner way to do this
Thought about, but did not try, an OnScrollListener, but this will not pre-buffer images until I start scrolling, which is not ideal
So, my questions are:
Is there a good way to detect when the last GridView item is requested to load? I found that the GridView.getlastvisibleposition() method is not useful here, because it is actually returning the last element for which Adapter.getView() has been called for. If I can do this accurately, I can launch the buffer request at that time
Is there a better way to do this?
you can do right this
if(GridView.getlastvisibleposition() = mAdapter.count()-1)
how you load the images?
is it from URL or from sdcard?
are you using a image loader library?

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.

Android Getting lag when loading items to a list view

I have a listview that loads images form urls
I get lag when when images are out of view. Meaning the ones That i have to scroll to see, seems to be loading from the internet while I am trying to scroll to them.
How can I let the listview load all my images at first run?
I know this is an old question, but for those who have a similar problem:
The Glide Library could help with it. I had this problem as well and I could solve it by using Glide. You can load an image like this:
Glide.with(mContext)
.asDrawable()
.load(mImageUrl)
.into(mViewHolder.mImageView);

Categories

Resources