Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android setImageURI out of memory error

I have a very small activity that must show an image.

If picture is not very small (for example 1.12 Mb 2560x1920) it produces out of memory on change screen orientation. I tried getDrawable.setCallback(null) but no luck.

Where am I wrong?

public class Fullscreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.gc();
    setContentView(R.layout.fullscreen);
    ImageView imageView = (ImageView) findViewById(R.id.full_screen_image);
    long imageId = 2;
    imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + imageId));
    }
}
like image 390
Boris Bondarenko Avatar asked Jun 11 '10 07:06

Boris Bondarenko


2 Answers

Try to add this to your onDestroy method:

ImageView imageView = (ImageView) findViewById(R.id.full_screen_image);
BitmapDrawable bd = (BitmapDrawable)imageView.getDrawable();
bd.getBitmap().recycle();
imageView.setImageBitmap(null);

It will recycle the bitmap used inside your ImageView.

like image 154
gingo Avatar answered Oct 01 '22 18:10

gingo


Consume less memory and downsample/resize(see documentation of BitmapOptions#inSampleSize) the picture.

like image 34
Samuh Avatar answered Oct 01 '22 17:10

Samuh