0

我在我的应用程序中使用了一些静态图像,图像被保存在可绘制文件夹中。图像的大小接近 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;
}
4

1 回答 1

0

首先压缩图像以检查这些图像是否真的是泄漏内存的图像。

习惯于使用工具来测量您正在使用的内存并发现泄漏,因为这只是一种方法。您可以从这里开始: 哪些 Android 工具和方法最适合查找内存/资源泄漏?

我还建议使用此代码查看图像 https://github.com/nostra13/Android-Universal-Image-Loader

于 2012-10-01T07:42:26.140 回答