-3

I have problem with out of memory with my bitmap. This is code:

Uri bitmapPictureUri = intent.getParcelableExtra(TaskActivity.PHOTO);
            Bitmap bitmap = null;

            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), bitmapPictureUri);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            int nh = (int) (bitmap.getHeight() * (512.0 / bitmap.getWidth()));
            bitmapPicture = Bitmap.createScaledBitmap(bitmap, 512, nh, true);

            picture.setImageBitmap(bitmapPicture);
            fileName.setText(tNameText+"_"+getCurrentTime());

everything is ok but when I change orientation I get outOfMemory. How can I solve my problem? I am thinking about softreference but I don't know how can I use it to bitmap. Any idea?

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
edi233
  • 3,511
  • 13
  • 56
  • 97
  • 1
    You need to read this dev guide section first: http://developer.android.com/training/displaying-bitmaps/index.html. Don't use `SoftReferences` as the GC is very aggressive with them and they are collected sooner than you believe. Instead of that use a `LRUCahce`. – gunar Jul 19 '13 at 11:23

1 Answers1

1

Recycle your bitmap when you start activity

    if(bitmap!=null){
         bitmap.recycle();
         bitmap=null;
    }

See this also

Community
  • 1
  • 1
T_V
  • 17,440
  • 6
  • 36
  • 48