Hi all,

I am using a gallery widget to display the pictures on the phone's
SDCard. Thus, in the getView method of the BaseAdapter, I am returning
the ImageView corresponding to the picture I want to show.

I read a lot of threads about performance or memory issues, and I
would like to share here all the precious pieces of information I
gathered so far.

 - 1. The getView method is called for each gallery item that is going
to be visible. Consequently, you may want to load images on demand. If
you don't keep a reference on the ImageView object, when becoming
invisible, this ImageView will be released (and its bitmap recycled I
suppose). I have not tested it yet, so please let me know if I'm
mistaken.

 - 2.  If it takes time to generate the bitmaps (the images might have
to be downloaded, or you are dealing with big pictures), you might
want to use a worker thread (or AsyncTask)  dedicated to creating the
bitmaps. This thread can use, as a data member, a Queue where you add
the work items to process. It will provide the user with a much
smoother UI. (please see the post by Romain Guy
http://android-developers.blogspot.com/2009/05/painless-threading.html
)

 - 3. You might want to cache the generated bitmaps because it is
costly to create them on demand all the time. Thus, if you generate a
100 x 100 pixels bitmap, it approximately contains 10 000 bytes
(10KB). I read that there is a maximum of 16MB per process in the
heap, but I don't think that you want to use the whole 16 MB. How much
memory should we use to cache our generated images? I'm not sure but I
will go back to this topic later on. Let's say for the moment that 2
MB is enough. Consequently, you could cache a maximum of 200 pictures.

At first, in order to display the images from the SDCard, I used the
Thumbnails content provider to get the URI of the picture thumbnails,
and created the corresponding bitmaps in a AsyncTask (as I described
in 2.). It worked but I realized that the performances were not as
good as the Gallery application (using a GridView) shipped with
Android. For each picture, the creation of its corresponding bitmap
(from the Thumbnails URI) took way more time than it did in the
Gallery Activity. Consequently, I checked the source code of the
Gallery Activity, and realized that it was using a thumbdata file in
SDCARD\DCIM\.thumbnails\thumbdata (something like that). This file
contains the bytes of a mini thumbnail (100 x 100) for each picture
taken on your phone. -  If you put pictures on your phone via the
SDCard directly, the first time you will try to browse the Gallery,
all the mini thumbnails will be generated and saved in that thumbdata
file. You will notice that the Gallery App will take a long time to
display the thumbnails (but only the first time). The second time, the
already existing thumbdata file will allow the Gallery to display
thumbnails very quickly. That is the secret of the Gallery Activity!

Consequently, I copied the methods used to read these bytes and tried
it on my gallery widget; it worked really well. It was way faster! As
you can imagine, the BitmapFactory.decodeFile executes faster with a
100 x 100 pixels file, than with a 300 x 200 pixels file (from
Thumbnails.EXTERNAL_CONTENT_URI). I am not sure about the exact size
of the thumbnails (I'd have to verify), but let's take that as an
example:
    - mini thumbnail in thumbdata: 100 x 100 = 10 000 bytes to decode
    - "normal" thumbnail in content provider: 300 x 200 = 60 000 bytes
to decode

I'm guessing the decoding speed of the BitmapFactory methods is
directly dependent on the number of bytes to iterate. So loading a
bitmap from the thumbdata file is 6 times faster than with a "normal"
thumbnail. That explains the great performances.

*** Questions ***

- Is there an API that allows us to get these generated thumbnails?
(Obviously I did not find one, but just in case..)
- Is there another fast way to get the pictures thumbnails? I could
still generate my mini thumbnails and store them in my own table or
own file, but because it is already done by the Gallery Activity, we
might as well use it!
- What do you guys think about the practice I described? Is it
something you would do?

Note: In the 3., I talked about caching the generated bitmaps. For
your information, based on my analysis of the open source code, the
Gallery Activity can cache a maximum of 500 bitmaps (again please let
me know if I'm mistaken), leading to a 5MB cache. When the 501st
bitmap is added to the cache, the first bitmap that was added is
recycled.

I hope this post might help some of us. I'm looking forward to reading
any opinion/answer about this topic.

Bye bye,
Jordan








--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to