0

I'm trying to create an image browser but I have an error when there are too much images to load

This is my code (the bitmap are used on the line 100) : http://pastebin.com/NidnH57b

If a try to access a directory with a lots of images I have an error.

Is there a solution to load a lots of images ?

Thanks

Ajouve
  • 9,735
  • 26
  • 90
  • 137

3 Answers3

1

I'd say that is no a good practice to load all the images you need at the beginning of the activity. Listviews for example only load the images that will be shown in your adapter. Yo should get all the information you need in an arraylist and use an arrayAdapter for example and it will destroy and recreate the views that are needed in order to save memory.

A simple way of doing this can be found here

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
0

Use MediaStore to get all of the device images getting through file iteration is very slow. You can get them like this:

Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.BUCKET_ID };
    String selection = MediaStore.Images.Media.BUCKET_ID + " = " + mAlbumId; //to get from specified folder
    Cursor cursor = getContentResolver().query(uri, projection, selection, null, null);

    while (cursor.moveToNext()) {
        Picture picture = new Picture();
        int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        picture.path = cursor.getString(columnIndex);

        columnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
        picture.id = cursor.getLong(columnIndex);

        columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
        picture.bucketId = cursor.getString(columnIndex);

        mAvailableImages.add(picture);
    }
    cursor.close();

Then use retrieved data in ListView with custom adapter and write your own ImageManager which will create and manage bitmaps for you.

You can read this and this topics.

Also look at Android native gallery http://viralpatel.net/blogs/pick-image-from-galary-android-app/

Community
  • 1
  • 1
Lingviston
  • 5,479
  • 5
  • 35
  • 67
0

You shouldn't preload all the images when you are accessing a directory. Instead you should load the image (asynchronous) in the getView of your adapter. Furthermore you might want to scale down the images based on their resolution instead of using a fixed value for inSampleSize. This site might help you: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Btw. you should also take a look at the ViewHolder-Pattern (e.g. here http://www.jmanzano.es/blog/?p=166).

fish
  • 828
  • 1
  • 6
  • 14