android中的Bitmap和outOfMemory

时间:2013-07-19 11:19:16

标签: android android-drawable

我的位图内存不足有问题。这是代码:

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());

一切都还可以,但是当我改变方向时,我会退出.OmMemory。我怎样才能解决我的问题?我正在考虑软参考,但我不知道如何使用它来进行位图。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

开始活动时回收您的位图

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

See this also