我有一个非常简单的应用程序,一个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这是应用程序应该做的唯一事情,我不是在创建其他对象,所以我想指出除了显示图像之外我不会将堆空间用于其他东西.