我在我的应用程序中使用了一些静态图像,图像被保存在drawable文件夹中。图像的大小接近2 MB,但我已正确缩放它们但仍显示内存错误运行时的位图大小。这是专为三星galaxy s3。 任何人都可以告诉我如何阻止这种情况并减少位图大小。
我已尝试使用此代码回收图片:
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);
}
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 = 8;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
答案 0 :(得分:0)
首先压缩图像以检查实际上是否存在泄漏记忆的图像。
习惯使用工具来测量正在使用的内存并发现泄漏,因为这只是一种方法。 你可以从这里开始: What Android tools and methods work best to find memory/resource leaks?
我还建议使用此代码进行图像查看 https://github.com/nostra13/Android-Universal-Image-Loader