Android smooth ListView scrolling while loading contact photos - android

I am using this method to load contacts photo, in my extended CursorAdapter (With LoaderManager):
private Bitmap loadContactPhoto(Uri contactUri) {
ContentResolver cr = getActivity().getContentResolver();
InputStream stream = Contacts.openContactPhotoInputStream(cr, contactUri);
return BitmapFactory.decodeStream(stream);
}
My contacts ListView using QuickContactBadge at each item, and the ListView does not scroll smooth, even if the contacts have no images.
Can I load the images in AsyncTask or make the loading faster?

Use this generally used method to heavily optimize your listviews via recycling:
Youtube
You should tailor the above method to work with the general method, ie only use the code you gave only so many times.
However, if you've already tailored the general optimization method to your needs as is and it's still quite slow, you SHOULD use an AsyncTask. The following link should get you set up- just use a placeholder image until the task is done, then save the image with the view object so you don't have to get the image again when the user re-scrolls through.
Making ListView Scrolling Smooth

Related

Custom SimpleCursor adapter BindView calls

I wanted to know if there is a way to make the BindView work only once per item?
Are items that we scroll away from the screen getting destroyed? and when they come back we must run bindview again?
The reason is I got my adapter to Download an image and set it on the item.
when i scroll the image is getting downloaded again, even if i didnt quit the application.
the bindview is called each time an item "returns" into display so even tho i already downloaded it, it will preform the asyntask again ( the asyntask is called in the adapter)
I even tried saving BLOB on my database after each download, to know when to call the asyntast and when just to make a bitmap out of the array
if(photoBArray != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(photoBArray, 0, photoBArray.length);
holder.icon.setImageBitmap(bitmap);
}else{
holder.icon.setImageResource(R.drawable.loader);
String urlString = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=100&photoreference="
+ c.getString(c.getColumnIndex(PlacesDBHelper.PHOTO_REFERENCE_COL))+"&key=API_KEY";
Log.e("Photo REFERENCE", c.getString(c.getColumnIndex(PlacesDBHelper.PHOTO_REFERENCE_COL)));
try {
URL url = new URL(urlString); // create new URL object from url string
new ImageDownloader(holder.icon, this).execute(url);
}catch (Exception e){
Log.e("Error in url", e.getMessage());
}
}
I am using chrisbanes pulltorefresh listview as well, im not sure if thats the casue for the problem
Bottom line question: how to make an item look steady and ready when he gets back into the screen and not beeign created again? ( cause even if i dont dowloand an image and only setting a bitmap, the action of setimagebitmap might be visible on a slow phone)
Thanks in advance!
The entire point of ListView is to recycle row views. They aren't destroyed, they are passed back to the adapter to be bound with new data for another row. It is expected that getView (or bindView in the case of cursor adapters) will be called again if you scroll an item off screen far enough and then scroll it back on screen.
What you need is an image caching layer between the download task and the adapter. When bindView happens, first check the cache to see if the image is there. If it is, use it. If it's not, download it and add it to the cache, then use it.

Android: GridView Adapter needs to download and display images with async tasks - too slow

I'm building an app that should make it possible to browse the images of an internet site with lots of galleries and photos. Here are the steps that the app is going through:
Extract URLs to albums and their images from rss feed
Build a ListView with every Album and one thumbnail
The ListView's adapter getView() method sets every item's ImageView to a placeholder, which will be replaced with the real thumbnail by the corresponding AsyncTask
The Adapter's getView() then executes an AsyncTask which fetches the image
The AsyncTask checks the cache first and if the picture is not there, it downloads
Using WeakReferences, the AsyncTasks update (or not) the desired View with the aquired image, thus replacing the placeholder.
When an Album is opened, I go through the same procedure as in step 2 but use a GridView instead of the ListView to display the album contents.
/* GridViewAdapter.getView(...) follows */
public View getView(int position, View convertView, ViewGroup parent)
ImageView thumbnail;
// Recycle or not...
if (convertView == null) {
// Create new View
thumbnail = new ImageView(mContext);
} else {
// Recycle View
thumbnail = (ImageView) convertView;
}
// Set the placeholder Drawable
thumbnail.setImageResource(R.drawable.placeholder);
if (position < amountOfPhotos) {
if (album.getLinks().size() >= 1) {
// imageFetcher creates an AsyncTask for every call of loadThumbnail(...)
imageFetcher.loadThumbnail(thumbnail, album.getURL(position));
}
// Formating the ImageView a little...
...
}
return thumbnail;
The problem is the performance of the AsyncTasks. The ListView displays on my phone around 7 items resulting in roughly a dozen simultaneous AsyncTasks when the user scrolls through the list. This is fine, the list still builds quickly.
Now the album's GridView displays 15 items at once and scrolling through the List creates many many AsyncTasks. Since some AsyncTasks have to download from the web, they stay alive for a couple of seconds. This completely slows out the AsyncTasks, which would only have to reload Bitmaps from cache.
The result is, that as long as many AsyncTasks are running, the GridView does not display images when scrolling back up, even if it just displayed them a few seconds ago. Simply because there are too many AsyncTasks.
Any suggestions on how to solve my problem? I was thinking of something like an AsyncTask factory, where I can queue jobs and set priorities. That way I could control which job is executed next and I could also control the maximum amount of AsyncTasks running at once.
It would already help me if someone could tell me if my approach sounds right in general, or if I'm completely on the wrong track with AsyncTasks here...
I suggest you to use a library like Picasso that's make all of your placeholder/async image downloading very, very easy !
Starting from Android 3.0 AsyncTasks are executed sequentially. So it's totally possible that some long-running AsyncTasks will block others. In order to execute the concurrently you can use asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
But generally a good solution will be to create a ThreadPoolExecutor and execute your Runnables on it. This way you can control the number of tasks running and decide which order to use.

Android List View

So basically I have a function which is something like that :
public static Bitmap getBitmapFromURL(String src)
, which return decrypted Bitmap.I need to be able to use that Bitmap into a Lazy loading ListView.
Example :
So I have a ListView.I'm downloading encrypted images and use that getBitmapFromURL function to return them as Bitmap,and after that I want to be able to reload the ListView with the new images which getBitmapFromURL returns to me.I want to find a way to save them in cache so when there is like 50 loaded jpg's in ListView I want to be able to delete the first loaded and keep only these which are visible while scrolling the ListView.And do exactly the same thing when I have another 50 loaded images.Any Suggestions how I can do this?
This is the well-known lazyList :
http://open-pim.com/tmp/LazyList.zip
I have created the gridView with spinner I will upload it tomorrow and make it available (: Maybe create a tutorial ... I am off now I will keep you in mind

Lazy-loading images in ListView on Android

I implemented the lazy-loading images in my ListView.
I use a AsyncTask to download the image from the internet and bind it to the ImageView in the UIThread.
It's working except that when I scroll the ListView vary fast, the downloaded images sometimes are binded into the wrong items in the list.
I guess the problem is from the reuse of convertView in the BaseAdapter.
Any ideas to solve it?
Many thanks.
EDIT:
I post the answer as following:
public void setBitmap(int position, Bitmap image) {
View itemView = mListView.getChildAt(position - mListView.getFirstVisiblePosition());
if (itemView != null) {
ImageView itemImageView = (ImageView) itemView.findViewById(R.id.item_imageview);
itemImageView.setImageBitmap(image);
}
}
There are two problems that will arise during lazy loading of images in a ListView.
The old images are still shown until the new ones are loaded. This is easy just set the ImageView to an image is loading view or set it to invisible before starting the image download.
The second problem is harder to solve. Imagine you are scrolling very fast through your list. Now your views may be recycled before the old AsyncTask has finished loading the image. You now have two tasks running that in the onPostExecute method will set an image to the imageview. Now for a short time the wrong image will be shown until the second Task finishes, or even worse for some network related reason they don't finish in the order they started and you have the wrong image overwrite the correct image. To solve this you have to check what image should be displayed after the task finished. In the View class are two methods for things exact like this one:
setTag and getTag You can bind any object to the imageview that comes into your mind. In most of the cases I use setTag to bind the URL of the image as a String to the imageview before I start a task. Now I can cast getTag to a String after the task finished and compare the URL that should be displayed with the URL that I downloaded and only set the image if necessary.
Create a function called void setBitmap(Bitmap bitmap, int position) or similar in your adapter. Let your AsyncTask call this method when a new bitmap is available. This method may then call notifyDataSetChanged() on the UI-Thread itself to ensure the views get refreshed. Holding references to views in an adapter (even by holding them in an AsyncTask) is dangerous!

Improve ListView efficiency when loading images from SD into the ListView

I am using a custom adapter for my ListView as per the efficient adapter sample by Romain Guy.
In the getView() method of my adapter I am assigning an ImageView a jpg image stored on SD using the following code :
File f=new File(MovieThumbs.get(position));
if(f.length() > 0) {
holder.thumb.setImageBitmap(BitmapFactory.decodeFile(MovieThumbs.get(position)));
}
When flicking through a list of some 200 items using this method the app suffers from bad stuttering as it tries dealing with the images.
Is there a more efficient solution for this?
Rather than loading the images from within the list adapter on demand how about kicking off a thread from the onCreate of your activity to load images? As each image loads you can fire a callback to the activity to display the image in the list. The callback method would be something along the lines of:
void onImageDownloadComplete(int pos, BitMap bm) {
ListView lv = getListView();
View listItem = lv.getChildAt(pos);
ImageView img = (ImageView)listItem.getChildAt(indexOfIcon);
img.setImageBitmap(bm);
}
Images need to be processed in background thread. Recycled views need to be taken into account. I try to address all these issues in my sample code, it works fine now, you may take a look Lazy load of images in ListView

Categories

Resources