0

getting out of memory issue

final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
                final String orderBy = MediaStore.Images.Media._ID;
                Cursor imagecursor = managedQuery(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                        null, orderBy);
                int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
                count = imagecursor.getCount();
                thumbnails = new Bitmap[count];
                arrPath = new String[count];
                thumbnailsselection = new boolean[count];
                for (int i = 0; i < count; i++) {
                    imagecursor.moveToPosition(i);
                    int id = imagecursor.getInt(image_column_index);
                    int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
                    thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                            getApplicationContext().getContentResolver(), id,
                            MediaStore.Images.Thumbnails.MICRO_KIND, null);
                    arrPath[i]= imagecursor.getString(dataColumnIndex);
                }
                GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
                imageAdapter = new ImageAdapter();
                imagegrid.setAdapter(imageAdapter);
                imagecursor.close();

getting Out of memory error here on bitmap:

Bitmap   thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                                getApplicationContext().getContentResolver(), id,
                                MediaStore.Images.Thumbnails.MICRO_KIND, null);

it crashing in some devices Please help

Nilesh Verma
  • 914
  • 1
  • 11
  • 26

2 Answers2

1

If you look at Android training manuals, there are a few documents telling you how to do this: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Personally, I would use Android Universal Image Loader to take care of that for you.

Anubhaw Arya
  • 145
  • 6
0

You need to compress the bitmap image using the compress method from the Android bitmap class. I ran into this issue a while back and ended up having to compress the images I was using..

Check out the description of the compress method at the link below. It should be fairly straightforward. You just choose your format from the enum's, the "quality", and the output stream.

Reference: http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)

philhan
  • 590
  • 1
  • 6
  • 19
  • I really don't have time to fire up Eclipse and find a line of code I wrote 3 years ago. Just use the reference above and Google around for an example. This is another example using `BitmapFactory.Options`: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – philhan Aug 20 '13 at 21:46