【发布时间】:2014-07-21 17:23:24
【问题描述】:
在我的应用程序中,我使用 SQLite 数据库来存储大量图像的数据。某些图像最大可达 600x600 像素。我使用自定义列表来创建位图。我知道有一个方法 bitmap.recycle();但我不确定如何将它与列表视图一起使用。
【问题讨论】:
标签: android memory-management android-listview bitmap out-of-memory
在我的应用程序中,我使用 SQLite 数据库来存储大量图像的数据。某些图像最大可达 600x600 像素。我使用自定义列表来创建位图。我知道有一个方法 bitmap.recycle();但我不确定如何将它与列表视图一起使用。
【问题讨论】:
标签: android memory-management android-listview bitmap out-of-memory
这里是在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);
}
下面是计算 InSampleSize(定义位图质量值(等级)的整数)的 util 函数。
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;
}
【讨论】: