Bitmap and outOfMemory in android

那年仲夏 提交于 2019-12-29 02:11:31

问题


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:


Recycle your bitmap when you start activity

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

See this also



来源:https://stackoverflow.com/questions/17744828/bitmap-and-outofmemory-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!