2011-10-20 122 views
1

我正在開發一個android應用程序,我必須將圖像從SD卡上傳到互聯網。對於這個我使用MediaStore內容提供商,正從MediaStore.Images.Media.DATA列和圖像路徑我現在做的是從使用Bitmap.decodeFile(字符串路徑路徑創建方法,然後在ImageView列表中顯示該位圖。我正面臨的問題是當我嘗試去顯示這個圖像列表中的活動我得到這樣的錯誤10-20 11:10:47.070:錯誤/ AndroidRuntime(922):java.lang.OutOfMemoryError :位圖大小超過虛擬機預算訪問SD卡圖像

有人可以建議我解決這個問題或其他方式來實現我的目標。提前致謝。

+0

這是更好地利用圖像代碼的大小調整前將圖像的ImageView – Pinki

+0

你的意思,我不能讓你。請多關注一下這個評論。 – Rocker

+0

調整所選圖像的大小。當你調整圖像大小,以減少大小,以避免內存不足異常 – Pinki

回答

1

這種情況很多時候會導致存儲在SD卡的圖像都是高分辨率的和您的應用程序無法處理圖像的大小爲堆大小

使用下面的一線得到位圖

Bitmap bmp = Bitmap.decodeFile(String path) 

你應該嘗試比例下來到您所需的大小

newBmp = Bitmap.createScaledBitmap(bmp, 240, 320, true); 

,然後使用newBmp在您的視圖中設置。

+0

感謝他的工作。 – Rocker

1

下面的代碼是用來調整所選圖像

Bitmap bitmap = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 
      int newWidth = 100; 
      int newHeight = 100; 

      // calculate the scale - in this case = 0.4f 
      float scaleWidth = ((float) newWidth)/width; 
      float scaleHeight = ((float) newHeight)/height; 

      // createa matrix for the manipulation 
      Matrix matrix = new Matrix(); 
      // resize the bit map 
      matrix.postScale(scaleWidth, scaleHeight); 

      Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
        width, height, matrix, true); 

      ((ImageView) findViewById(R.id.ImageviewTest)).setImageBitmap(resizedBitmap); 
0
String filepath = Environment.getExternalStorageDirectory() 
      .getAbsolutePath(); 
    File file = new File(filepath, filename); 
    FileInputStream fs; 
    try { 
     fs = new FileInputStream(file); 
     BitmapFactory.Options bfOptions = new BitmapFactory.Options(); 
     /* 
     * bfOptions.inDither=false; //Disable Dithering mode 
     * bfOptions.inPurgeable=true; //Tell to gc that whether it needs 
     * free memory, the Bitmap can be cleared 
     * bfOptions.inInputShareable=true; 
     */ 
     bfOptions.inJustDecodeBounds = false; 
     bfOptions.inTempStorage = new byte[32 * 1024]; 
     Bitmap b = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, 
       bfOptions); 
     img.setImageBitmap(b); 
} 
catch() 
{ 
}