我有一个非常简单的应用程序,其中包含一个 ImageView 和一个 Button。我的ImageView加载的第一个 Drawable 资源是用 XML 布局中的“android:src”标签指定的,但是在运行时我想更改它显示的图片。为此,我启动了一个 Activity 以从 sd 卡中选择图像(意图发送到 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-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
}
}
ps 这是应用程序唯一应该做的事情,我没有创建其他对象,所以我想指出,除了显示图像之外,我没有将堆空间用于其他东西。