2011-07-25 33 views
1

可能重复:
OutOfMemoryError: bitmap size exceeds VM budget :- AndroidOutOfMemory例外时的处理的图像

林书面其使用图像从库中的程序的过程,并然后将它们显示在一个活动(一个图像公关活动)。不过我已经碰到这个错误一遍又一遍三天直而不做消除它的任何进展:

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方法是从here和resolveOrientation方法只是包装位图到矩阵,顺时针旋转90度使它转动。

我真的很希望有人能帮我解决这件事。

+0

重复http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android或http://stackoverflow.com/questions/6131927/bitmap-size-exceeds-vm - 在Android预算? – THelper

+0

@THelper:你知道如何解决这个问题吗?根据您提供的两个链接,我已经实施了'解决方案',但它没有帮助 – Arcadia

回答

2

那是因为你的位图尺寸较大,所以手动缩小图像尺寸,或通过编程

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 8; 
Bitmap preview_bitmap = BitmapFactory.decodeFile(mPathName, options); 
+0

谢谢。它帮助^^ – Arcadia

1

您的GC不会运行。尝试通过作品让你的位图

BitmapFactory.Options buffer = new BitmapFactory.Options(); 
buffer.inSampleSize = 4; 
Bitmap bmp = BitmapFactory.decodeFile(path, buffer); 
+0

谢谢..它帮助:) – Arcadia

1

有许多问题在#1约bitmap size exceeds VM budget所以首先搜索关于您的问题,当你找不到那么任何解决方案在这里问的问题

1

问题是因为你的位图的大小比VM能处理的还要大。例如,从您的代码中,我可以看到您正尝试将图像粘贴到使用Camera捕获的imageView中。所以通常情况下,相机图像的尺寸太大会明显增加这个误差。 正如其他人所建议的那样,您必须通过对图像进行采样或将图像转换为较小的分辨率来压缩图像。 例如,如果您的imageView的宽度和高度是100x100,则可以创建缩放的位图,以便您的imageView得到精确填充。你可以这样做,

Bitmap newImage = Bitmap.createScaledBitmap(bm, 350, 300,true); 

或者你可以在用户hotveryspicy建议的方法中对它进行采样。