1

I have a photo editing app. When I import an image, it works fine but, opening up the image for editing causes it to crash.

This is the logcat output:


08-04 20:56:16.973: E/dalvikvm-heap(336): 810000-byte external allocation too large for this process.
08-04 20:56:17.073: I/dalvikvm-heap(336): Clamp target GC heap from 25.289MB to 24.000MB
08-04 20:56:17.073: E/GraphicsJNI(336): VM won't let us allocate 810000 bytes

UPDATE::

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.ivPhoto);
        chosenBitmap = superDecodeFile(new File(picturePath));
        imageView.setBackgroundColor(0);
        Bitmap bMap =   Bitmap.createScaledBitmap(chosenBitmap, 500, 500, true);
        imageView.setImageBitmap(bMap);
    }

}

this is my onResult code .. so the choosenBitmap displays on the imageView ( ivPhoto ) I need to use .recycle(); so i free up some memory by recycling the already imageView .. where do I use the .recycle(); ? I tried to change the .setBackgroundColor(0); to .recycle(); but it doesn't work

John Jared
  • 790
  • 3
  • 16
  • 40

2 Answers2

3

This is a memory leak error.

Your app is allocated a heap of size ~24MB. But the image you are trying to edit is larger than the heap size.

This is happening because you are not freeing up the memory. Android's Dalvik VM does not take care of GC-ing the native memory used by the app. So, when dealing with images in Android, you need to explicitly use recycle(). This frees up the native memory.

For more deatils: Changing ImageView content causes OutOfMemoryError

Community
  • 1
  • 1
Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72
  • You need to recycle the Bitmap image object. So, after imageView.setBitmap(), call bMap.recycle(). For more details: http://stackoverflow.com/questions/3823799/android-bitmap-recycle-how-does-it-work – Sagar Hatekar Aug 04 '12 at 22:52
  • when I call recycle after imageBiew.setBitmap it force closes – John Jared Aug 04 '12 at 23:06
  • 1
    What happens if you did just System.GC(). Comment the recycle() for now. I hope I have helped you pinpoint the cause for your problem. You might have to do some digging online to learn how to recycle/GC images. – Sagar Hatekar Aug 04 '12 at 23:20
0

Your image is too large to load into memory. Unfortunately you cannot change the maximum amount of memory your application uses. See Displaying Bitmaps for detailed information about loading and displaying bitmaps.

Frohnzie
  • 3,559
  • 1
  • 21
  • 24