2011-07-25 32 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建議的方法中對它進行採樣。