Bitmap and outOfMemory in android

2020-02-07 13:08发布

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?

1条回答
狗以群分
2楼-- · 2020-02-07 13:42

Recycle your bitmap when you start activity

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

See this also

查看更多
登录 后发表回答