7

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));
    }
}
Boris Bondarenko
  • 153
  • 1
  • 2
  • 7

4 Answers4

11

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.

gingo
  • 3,149
  • 1
  • 23
  • 32
  • 3
    great post but you could add an obligatory `if != null` check for bd :) – Ron May 27 '13 at 14:43
  • It is not necessary if you always set drawable in your onCreate ;) – gingo Jun 18 '13 at 09:42
  • recycle seems a very old method. Is it necessary on Android 4.0? – LiangWang Jan 21 '15 at 12:59
  • Ron & gingo, thank you. And it's best if null check is included. Also, when returning from another activity, usually camera, the image view should call setImageBitmap(null) and destroyDrawingCache() to display the new image. – iSofia Sep 03 '17 at 18:05
4

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

Samuh
  • 36,316
  • 26
  • 109
  • 116
1

You could also use something like this:

        File picture = new File("path_to_image");
        if (picture.exists()) {
            ImageView imageView = (ImageView)findViewById(R.id.imageView);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            Bitmap myBitmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), options);
            imageView.setImageBitmap(myBitmap);
        }

Read the following link for more information about the BitmapFactory options (especially inSampleSize, which controls the degree of subsampling): http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

1

Your application must be leaking context. That's usually the reason why application crashes after several orientation changes. Read this carefully http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html.

Fedor
  • 43,261
  • 10
  • 79
  • 89