2011-11-03 67 views
1

我在ImageActivity上有一個按鈕ImageView &。當點擊按鈕時,列出可用圖像,當點擊它們中的一個時,它被加載到ImageView中。java OutOfMemoryError:位圖大小超過VM預算

ImageView的XML:

<ImageView 
     android:id="@+id/imageView" 
     android:layout_width="300dip" 
     android:layout_height="300dip" 
     android:src="@android:drawable/alert_light_frame" 
    /> 

請參閱代碼:

public class ImageActivity extends Activity { 

    private static final int SELECT_PICTURE = 1; 
    private String selectedImagePath; 
    private ImageView imageView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.comments_detail); 

    imageView = (ImageView) findViewById(R.id.imageView); 

     ((Button) findViewById(R.id.BtnBrowse)) 
     .setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View arg0) 
      { 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE); 
     } 
     }); 
} 

    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (resultCode == RESULT_OK) 
     { 
      if (requestCode == SELECT_PICTURE) 
      { 
       Uri selectedImageUri = data.getData(); 
       selectedImagePath = getPath(selectedImageUri); 
       //System.out.println("Image Path : " + selectedImagePath); 
       imageView.setImageURI(selectedImageUri); 
      } 
     } 
    } 

    public String getPath(Uri uri) 
    { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 


} 

問題:的問題是,當我點擊該按鈕會顯示可用的圖像,我選擇其中之一&它加載在ImageView中,當我重複這些步驟幾次應用程序拋出異常時: java.lang.OutOfMemoryError:位圖大小超過VM預算

我戴上它&發現這個問題是由於內存泄漏,但我無法找到我的代碼有什麼問題。任何人都可以花一些他寶貴的時間來幫助我嗎?

謝謝。

+0

你爲什麼需要繼續參考imageView? – bestsss

+0

謝謝@bestsss 我需要將選定的圖像保存到SQLite數據庫 –

+0

,但你有2個圖像在同一時間,有效地加倍所需的內存。解決使用前的圖像,而不是創建... – bestsss

回答

-1

那麼打電話system.gc()工作! 圖像分配給imageView之前,我打電話給System.gc(),照顧它的工作內存&。

看修改後的代碼:

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (resultCode == RESULT_OK) 
    { 
     if (requestCode == SELECT_PICTURE) 
     { 
      // calling GC to take care of memory, if you remove this one line of code 
      // application will throw the error "VM out of memory error" 
      System.gc(); 
      Uri selectedImageUri = data.getData(); 
      selectedImagePath = getPath(selectedImageUri); 
      imageView.setImageURI(selectedImageUri); 
     } 
    } 
} 

感謝@Torid

有人可以對此發表評論嗎?在這種情況下調用GC是否正確?

1

如果您執行一系列setImageURIs,則您正在將一系列位圖加載到視圖中。如果您獲得java.lang.OutOfMemoryError: bitmap size exceeds VM budget exception,這意味着imageView在加載新位圖時不會回收以前的位圖。所以,你

  • 要麼需要鼓勵的ImageView回收之前的位圖 - 與setImageView("")
  • 可能還是從URI獲得的位圖和使用setImageBitmap(android.graphics.Bitmap)代替;那麼你可以做setImageBitmap(null)bitmap.recycle()
+0

謝謝@Torid 讓我試試吧。 –

+0

bestsss的建議做imageView.setBitmap(Bitmap.createImageBitmap(1,1,Bitmap.Config.ARGB_8888))是你應該如何鼓勵imageView回收(如果你去那條路線)。 – Torid

+0

我試過'imageView.setImageBitmap(Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888));'但結果是一樣的 –

相關問題