Sol*_*lin 20 android android-widget
我有一个非常简单的应用程序,一个ImageView和一个Button.我的ImageView加载的第一个Drawable资源在XML Layout中使用"android:src"标记指定,但是在运行时我想要更改它显示的图片.为此,我启动一个活动结果从SD卡中选择一个图像(意图发送到MediaStore.Images.Media.EXTERNAL_CONTENT_URI).但是,当选择图片时,我尝试使用所选图片的URI更新ImageView,但我收到消息" java.lang.OutOfMemoryError:位图大小超过VM预算 "
我试图用我的HTC-Hero加载用相机拍摄的照片(照片大小约为1.1M),但没有成功,似乎只适用于小于500KB的照片.但是我需要加载用相机拍摄的照片.我怎么解决这个问题?我究竟做错了什么.在我看来,代码非常简单,应该可以工作.
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
}
}
Run Code Online (Sandbox Code Playgroud)
ps这是应用程序应该做的唯一事情,我不是在创建其他对象,所以我想指出除了显示图像之外我不会将堆空间用于其他东西.
Sol*_*lin 43
似乎在加载新的Bitmap之前我必须回收ImageView显示的Bitmap,现在它正在运行没有问题.希望这有助于其他人,只需在设置新的ImageView内容之前调用下面的方法.
((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
所以代码现在看起来像这样:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意在ImageView的位图上调用再循环.
这让我疯了.
我正在建造一个具有非常大的高分辨率全屏图像(800x1232)的Xoom.
以下代码对我有用:
public void onStop()
{
super.onStop();
imageView.setImageBitmap(null);
}
Run Code Online (Sandbox Code Playgroud)
祝好运!!!
| 归档时间: |
|
| 查看次数: |
17285 次 |
| 最近记录: |