2013-04-09 102 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