user3437721
user3437721

Reputation: 2289

Lazyload with a Bitmap Array

So my app lists all the files and folders in a given Dropbox folder. They are listed in a custom ListView using an adapter.

Currently I use drawables for the image and folder icons. I loop through the Dropbox folder structure and add the needed drawable to a Bitmap array. This is done in an Async Task (doInBackground).

I then call the Adapter in the PostExecute like this :

adapter = new ImageAdapter(this, pix, paths);
 lstView.setAdapter(adapter);

This then shows all the files and images (with default drawable icons) in the ListView.

The next step I want to do is start loading the thumbnails from dropbox. So for every image in the List, I want to replace the drawable with a thumbnail retreived from dropbox. Again this should be done in an Async task so the user can still scroll through the listview.

With dropbox, you can load thumbails like this:

if(fileInfo.thumbExists)
                        {
                           file = fileSystem.openThumbnail(fileInfo.path, ThumbSize.XS, ThumbFormat.PNG);

                            Bitmap image = BitmapFactory.decodeStream(file.getReadStream());
                            thumbs.add(image);
                            file.close();
                        }

In the code above, thumbs is a Bitmap Array.

I was planning on using Universal Image Loader or Picasso. But you cannot pass in a Bitmap Array into either of those. It has to be a URL or URI.

How can I achieve this? I'm guessing I need another async task, but I'm not sure how to update my adapter.

Considerations:

Remember - I am using an array of Bitmaps, I don't have any URLS. Would I be best saving each Bitmap to the sd card and then using UIL or Picasso to load using the URI? But how would you know which images went to which position in the ListView?

SO the steps in my code would ideally be:

-Load the Listview with the files and folders with dummy images (already doing this!)

-Get the thumbnails from the decodeStream and load into the Bitmap Array

-Load the thumbnails into the correct position into the ListView

Upvotes: 1

Views: 216

Answers (1)

Parth Kapoor
Parth Kapoor

Reputation: 1504

For each row of the list view, run a separate thread to download and show the thumbnail image. I guess, you can avoid the use of a bitmap array and directly feed the downloaded image to the list view row.

I) Modify your getView() call back of the adapter like the one below:

ImageView thumbnail=Container.findViewById(R.id.thumbnail) loadImageInBackground(thumbnail,URL).execute();

II) loadImageInBackground will be a class that implements AsyncTask

a) data members: ImageView thumbnail,String URL

b) Constructor: Initialize these data members with instances passed.

c) onPostExecute: Set a dummy image to the thumbnail here, that will appear until the required image is downloaded.You can even set an animating image here to give it a better feel.

d) doInBackground:

                mUrl = new URL(strUrl);
                HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection();   
                conn.setDoInput(true);   
                conn.connect();     
                InputStream is = conn.getInputStream();
                bmImg = BitmapFactory.decodeStream(is); 
                is.close();

            }
            catch (IOException e)
            {       
                e.printStackTrace();  
            }

e) onPostExecute: associate the this.thumbnail to this.bmImg.

This way for each row of your list view an object of loadImageInBackground class will be initialized and will download the image from URL and set it to the image View container of the corresponding row.

The images will be displayed on the Thumbnail as soon as they are downloaded. There will be no Blocking, I mean, the user can interact with the app even when the images are being downloaded. If you have implemented a lazy loading methodology, then this approach will not cause any outOfMemory exceptions in any case.And also, new images will only be downloaded when the user scrolls. Point II.c provides for setting loading animation/image on the thumbnail until the actual image has been downloaded.

Upvotes: 0

Related Questions