【问题标题】:BitmapFactory.decodeFile out of memory with images 2400x2400BitmapFactory.decodeFile 内存不足,图像 2400x2400
【发布时间】:2013-11-09 19:33:19
【问题描述】:

我需要将图像从文件发送到服务器。服务器请求分辨率为 2400x2400 的图像。

我想做的是:

1) 使用正确的 inSampleSize 使用 BitmapFactory.decodeFile 获取位图。

2) 以 JPEG 格式压缩图像,质量为 40%

3) 对图片进行base64编码

4) 发送到服务器

我无法完成第一步,它会引发内存不足异常。我确定 inSampleSize 是正确的,但我想即使使用 inSampleSize 位图也很大(DDMS 中大约 30 MB)。

任何想法怎么做?我可以在不创建位图对象的情况下执行这些步骤吗?我的意思是在文件系统而不是 RAM 内存上进行。

这是当前代码:

// The following function calculate the correct inSampleSize
Bitmap image = Util.decodeSampledBitmapFromFile(imagePath, width,height);   
// compressing the image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 40, baos);
// encode image
String encodedImage = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));


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) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromFile(String path,int reqWidth, int reqHeight) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path,options);
}

【问题讨论】:

  • getBitmap().recycle();
  • 您在文件中有图像,对吗?只需将此文件发送到服务器并在那里进行必要的转换。
  • 我无法向服务器发送 12MB 的数据,调整大小和压缩图像大大减小了大小,小于 1MB

标签: android bitmap


【解决方案1】:

您可以跳过 ARGB_8888,而使用 RGB_565,然后抖动图像以保持良好的质量

 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = false;
 options.inPreferredConfig = Config.RGB_565;
 options.inDither = true;

【讨论】:

  • 它将位图的大小减小了 13 MB 并且它正在工作。非常感谢!
【解决方案2】:

您必须使用BitmapFactory.Options 并将inJustDecodeBounds 设置为true。这样您就可以加载有关位图的信息并计算对其进行下采样的值(例如inSampleSize

【讨论】:

  • 我想看看。你能用代码的sn-p更新帖子吗?
【解决方案3】:

不要将图像加载为位图,将其转换为数组,然后发送。

改为:

以 JPG 格式将其作为文件读取。使用 files 字节数组,对其进行编码,然后发送文件。

将其加载到位图会导致不必要的巨大内存问题。以位图格式表示的图像将占用大约 20 倍或更多内存。

在服务器端,您也需要将其视为文件。而不是位图。

这是一个将文件加载到 byte[] 的链接:Elegant way to read file into byte[] array in Java

【讨论】:

  • 我需要一个位图,因为我需要调整它的大小并压缩它。原始图像大小为 12MB,我想发送到服务器大约 700KB,而不是原始图像的 12MB。
  • @alasarr 我错了。你一定是采样错了。见developer.android.com/training/displaying-bitmaps/…
  • @alasarr 让您知道。位图格式的2400x2400 图像,每个像素使用2 bytes,大小为11MB。无论jpg格式是否为700KB。或者在4 byte 表示是22MB
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 2013-12-20
  • 2011-12-05
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多