0

The following codes were used to retrieve all photos for the purpose of displaying on gridview. However, if i have 1000 photos, it would result in out of memory error. Is there anyone who could help with this?

P.S. if anyone could, are u able to show the edited codes for the above to make use of lazy loading and caching? I'm pretty lost. The initialise method is used basically to set the ImageAdapter that will be used by gridview

Thanks!!

    public void initialize() {
        images.clear();
        final String[] columns = { MediaStore.Images.Thumbnails._ID };
        final String orderBy = MediaStore.Images.Media._ID;
        @SuppressWarnings("deprecation")
        Cursor imagecursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                 null, null, orderBy);

        if(imagecursor != null){
            int image_column_index = imagecursor
                    .getColumnIndex(MediaStore.Images.Media._ID);
            int count = imagecursor.getCount();
            for (int i = 0; i < count; i++) {
                imagecursor.moveToPosition(i);
                int id = imagecursor.getInt(image_column_index);
                ImageItem imageItem = new ImageItem();
                imageItem.id = id;
                lastId = id;
                imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
                        getApplicationContext().getContentResolver(), id,
                        MediaStore.Images.Thumbnails.MICRO_KIND, null);
                images.add(imageItem);
            }
            //imagecursor.close();
        }
        notifyDataSetChanged();
    }
iceraven
  • 225
  • 1
  • 5
  • 16
  • 3
    A single view with 1000 images seems... unusable. Why would you display that many images at once? – Wooble Jan 28 '13 at 19:49
  • 1
    Do you need to load all the images at the beginning, or can you load dynamically? For example, if you display 4 images on the screen you might load 12 (previous screen, current screen, next screen). – thegrinner Jan 28 '13 at 19:51
  • try with a lazy loader http://stackoverflow.com/q/541966/2015318 – StarsSky Jan 28 '13 at 19:52
  • if anyone could, are u able to show the codes for the above to make use of lazy loading and caching? I'm pretty lost. The initialise method is used basically to set the ImageAdapter that will be used by gridview. – iceraven Jan 30 '13 at 04:40

1 Answers1

3

You're running out of memory because you're loading lots of bitmaps that take up lots of memory. The simple (and correct) answer is: don't load them all at once. Instead you should load each image in the adapter as necessary.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • 3
    Best to use a CursorAdapter in this case. – Yaroslav Mytkalyk Jan 28 '13 at 19:54
  • if anyone could, are u able to show the edited codes for the above to make use of lazy loading and caching? I'm pretty lost. The initialise method is used basically to set the ImageAdapter that will be used by gridview. – iceraven Jan 30 '13 at 04:40