0

在我的应用程序中,我使用SQLite数据库来存储大量图像的数据。某些图像可以达到600x600像素。我使用自定义列表来创建位图。我知道有一个方法bitmap.recycle();但我不知道如何在listview中使用它。OutOfMemoryError android bitmap listview

+0

的可能重复的[奇异出存储器问题在加载图像以Bitmap对象(http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading -an-image-to-a-bitmap-object) – tyczj

回答

0

这里是处理位图在Android中

首先,你要计算出位的sampleBitmapSize的解决方案(加载相同的位图的较低版本)

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
     int reqWidth, int reqHeight) { 

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

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

下面是UTIL功能计算InSampleSize(一个定义位图质量值(评分)的整数)。

public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    Log.i("ImageUtil", "InSampleSize: "+inSampleSize); 
    return inSampleSize; 
} 
+0

使用这种技术,我可以在非UI线程上轻松处理25kx25k像素的图像。 –

+0

谢谢,并且在reqWidth和reqHeight参数中,我应该放置所需的大小,例如600x600? –

+0

是的,它是你想要的位图的高度和宽度(原始位图的较小版本)。 –