改变ImageView的内容导致的OutOfMemoryError内容、ImageView、OutOfMemoryError

2023-09-05 07:42:07 作者:单身菇凉╆照样嗨i

我有一个一个的ImageView和一个按钮非常简单的应用程序。通过加载的第一个绘制资源我的的ImageView 指定的机器人:SRC在XML布局的标签,但在运行时我想更改它所显示的图片。要做到这一点我开始一个活动的结果,以从SD卡(意向发送到MediaStore.Images.Media.EXTERNAL_CONTENT_URI)的图像。然而,当选择了图片,我尝试更新与所选图片的URI ImageView的,但我得到的消息 java.lang.OutOfMemoryError:位图大小超过VM预算

I have a very simple application with one ImageView and a Button. The first Drawable resource loaded by my ImageView is specified with the "android:src" tag in the XML Layout, however at runtime i want to change the picture displayed by it. To do so i start an Activity for result to pick an image from the sd card (intent sent to MediaStore.Images.Media.EXTERNAL_CONTENT_URI). However when the picture is selected, i try to update the ImageView with the chosen Picture's URI but i get the message "java.lang.OutOfMemoryError: bitmap size exceeds VM budget"

我'尝试加载与我的HTC-英雄的摄像头(图片大小是1.1M左右),但没有成功拍摄的照片,似乎只用图片是小于500KB.However我需要加载拍摄的照片工作用相机。 我该如何解决呢?我在做什么错。在我看来,该code是非常简单的,应该工作。

I'am trying to load pictures taken with the camera (pics size are around 1.1M) of my HTC-Hero but no success, seems to work only with pictures that are less than 500KB.However i need to load pictures taken with the camera. How can i solve this? what am I doing wrong. Seem to me that the code is very simple and should work.

public void onClick(View v){
 Intent selectImageIntent=new Intent(Intent.ACTION_PICK , 
   android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
   startActivityForResult(selectImageIntent,1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
 super.onActivityResult(requestCode, resultCode, data);
 if(resultCode==Activity.RESULT_OK){
   Uri selectedImageUri = data.getData();
   Log.i(TAG,"chosen image: "+selectedImageUri.toString());
   ImageView imageView = (ImageView) this.findViewById(R.id.ImageView01);
   imageView.setImageURI(selectedImageUri);//here I get the OutOfMemoryError
   imageView.invalidate();

 }else{
  //canceled
 }

}

P.S。这是唯一的应用程序应该做的,我不会创建其他对象,因此,我会想指出的是,我不使用的堆空间,其他的东西,除了显示图像。

p.s. that is the only thing the App should do, I'm not creating other objects so I will like to point out that I am not using heap space for other stuff besides displaying the image.

推荐答案

好像装了新的位图,我不得不回收了ImageView的显示位图前,现在它正在没有问题。希望这可以帮助别人,设置新的ImageView内容之前只需要调用如下因素的方法。

Seems like before loading a new Bitmap I had to recycle the Bitmap displayed by the ImageView, now it is working without a problem. Hope this helps someone else, just call the folowing method before setting the new ImageView's content.

((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();

因此​​code现在看起来是这样的:

so the code now looks like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
        case INTENT_REQUEST_SELECT_IMAGE:
            if(resultCode==Activity.RESULT_OK){
                Uri selectedImageUri = data.getData();
                Log.i(TAG,"selectedImageUri.getPath()"+selectedImageUri.getPath() );
                ImageView imageView = (ImageView) this.findViewById(R.id.ImageView_of_text);
                ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
                imageView.setImageURI(selectedImageUri);
                imageView.invalidate();

            }else{
                //TODO define what to do in case the user canceled OCR or any other event
            }
            break;
        default:
            break;
    }
}

请注意调用回收上的ImageView的位图。

Please note the call to recycle on the Bitmap of the ImageView.