我有一個非常簡單的應用程序,只有一個ImageView和一個按鈕。我的ImageView加載的第一個Drawable資源是在XML Layout中的「android:src」標籤中指定的,但是在運行時我想更改它顯示的圖片。要做到這一點,我開始一個活動的結果從SD卡中選擇一個圖像(意圖發送到MediaStore.Images.Media.EXTERNAL_CONTENT_URI)。被選中的圖片然而,當我嘗試更新與所選擇的圖片的URI的ImageView的,但我得到的消息「java.lang.OutOfMemoryError:位圖大小超過VM預算」更改ImageView內容導致OutOfMemoryError
我'嘗試加載拍攝的照片與我的HTC - 英雄的照相機(圖片大小約爲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
}
}
p.s.這是應用程序應該做的唯一的事情,我不會創建其他對象,所以我想指出,除了顯示圖像外,我沒有爲其他內容使用堆空間。
如果對ImageView使用FragmentStatePagerAdapter,則此方法將節省您的時間,因爲回收位圖時會重新膨脹視圖時會導致錯誤。 – 2012-04-27 00:17:29