3

I have a sqlite database that has over 2000 low res thumbnails of pictures. The database itself is over 100MB in size. The project requires me to have the photos residing in the database instead of the file path.

This is what I would like to do and need help with as this is my first time dealing with images. I have a horizontal GalleryView in my App UI that I want to populate with images pulled from this database dynamically. I don't mind if these images are lazy loaded on to the gallery view in the background. I read somewhere that I will have to use an ImageAdapter to bind the images to the UI element dynamically. I need in understanding how this is done. I have never used the BaseAdapter class.

Thanks, AB

Abhishek Sharma
  • 609
  • 1
  • 11
  • 22

2 Answers2

1

You will indeed be subclassing an Adapter to pull the images out of your db turn them into Bitmaps or Drawables then set your picture to an ImageView and add it to your Gallery.

I would suggest you switch away from using a Gallery right now before you get going though. Gallery has a bug where it will not allow your Adapter to recycle views properly. This means while the user is scrolling the gallery every time a new Image enters the screen a new ImageView object is getting created. This is wasteful and will lead to noticeable performance drop while your gallery is being scrolled due to garbage collection constantly running.

To get started with your adapter check out SimpleCursorAdapter and search around online for examples of how to extend SimpleCursorAdapter. Inside your getView method you'll pull your image out of the db and set it into a view to be shown to the user.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Thanks for the quick update Tim. However, if I were to leave the GalleryView as is in the UI and give user access to it only after all the pictures from the db have been loaded, how would I go about doing that? Unfortunately, I don't have the option to get rid of the horizontal Gallery type view for my Images. – Abhishek Sharma Oct 12 '11 at 20:04
  • In other words, do you think loading the 2000+ images to the gallery will take a huge amount of time? – Abhishek Sharma Oct 12 '11 at 20:07
  • It will not load all 2000+ at once. It will load what it needs only to fill up the screen. Then when the user scrolls far enough that a new one is showing it'll load that new one. So it doesn't preload everything just what is on the screen. – FoamyGuy Oct 12 '11 at 21:33
0

I was able to do this using a custom adapter and a string array that holds the location of the images on the sdcard. Before doing all this though, I have to pull images from the database and copy them over to an sdcard location. I am working on avoiding this step as it adds up 5-7 secs to just copy those images over.

Abhishek Sharma
  • 609
  • 1
  • 11
  • 22