2013-04-09 159 views
0

我下載從我的服務器大約100張序,以顯示他們在ListView,圖片加載SD卡從

因此我將它們保存在SD卡我,我不會收到OutOfMemoryException異常。但是,我發現即使我將它們下載到SD卡,我也必須將它們解碼爲需要大量內存然後顯示它們的位圖,因此我還得到了「OutOfMemory」異常。

有什麼需要處理的嗎?

非常感謝

這是我的代碼從SD卡加載圖像:

Bitmap bmImg = BitmapFactory.decodeFile("path of img1"); 
imageView.setImageBitmap(bmImg); 

回答

2

嘗試使用此代碼從文件加載圖像:

img.setImageBitmap(decodeSampledBitmapFromFile(imagePath, 1000, 700)); 

decodeSampledBitmapFromFile

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ // BEST QUALITY MATCH 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     options.inPreferredConfig = Bitmap.Config.RGB_565; 
     int inSampleSize = 1; 

     if (height > reqHeight) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } 

     int expectedWidth = width/inSampleSize; 

     if (expectedWidth > reqWidth) { 
      //if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize.. 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    options.inSampleSize = inSampleSize; 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 

    return BitmapFactory.decodeFile(path, options); 
    } 

您可以使用數字(在這種情況下爲1000,700)來配置圖像文件輸出的質量。

+0

非常感謝,我會試試看,還有什麼是推薦的參數?那圖像不會那麼大,而且還保持良好的品質? – 2013-04-09 22:24:01

+0

取決於你想應用這個圖像的ImageView的大小... – 2013-04-09 22:24:59

+0

如果我想要的圖像,直到0.2MByte,我也可以得到一個很好的決議?也許你知道其他方式,而不使用位圖?或使用方式從SD卡加載圖像而不加載到我的應用程序數據空間? – 2013-04-09 22:28:34