我正在编写一个程序,该程序使用来自画廊的图像,然后在一个活动中显示它们(每个活动一个图像)。但是,我连续三天都遇到了这个错误,一直无法解决它,这是错误信息:OutOfMemoryError: bitmap size exceeds VM budget :- Android。
在onActivityResult方法中,我有以下内容:
07-25 11:43:36.197: ERROR/AndroidRuntime(346): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
我的代码流程如下:
当用户按下按钮时,会触发一个意图,进入相册:
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 0);
用户选择了一张图片后,该图片会在一个ImageView中呈现:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:background="#ffffffff"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:maxWidth="250dip"
android:maxHeight="250dip"
android:adjustViewBounds="true"/>
</LinearLayout>
在onActivityResult方法中,我有以下内容:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case 0: // Gallery
String realPath = getRealPathFromURI(data.getData());
File imgFile = new File(realPath);
Bitmap myBitmap;
try {
myBitmap = decodeFile(imgFile);
Bitmap rotatedBitmap = resolveOrientation(myBitmap);
img.setImageBitmap(rotatedBitmap);
OPTIONS_TYPE = 1;
} catch (IOException e) { e.printStackTrace(); }
insertImageInDB(realPath);
break;
case 1: // Camera
decodeFile方法来自于这里,而resolveOrientation方法只是将位图包装到矩阵中并顺时针旋转90度。希望有人能帮我解决这个问题。