【问题标题】:Android getting java.lang.OutOfMemoryError when resizing an image调整图像大小时Android得到java.lang.OutOfMemoryError
【发布时间】:2017-11-29 22:03:21
【问题描述】:

我有一个在我的手机上运行良好的。但是,当我检查实时崩溃数据时,人们会收到内存不足错误。

java.lang.OutOfMemoryError

我正在尝试从照片中加载图像并尝试使图像更小。

这是我的代码

所需大小为 1000

 public static Bitmap getImage(Context c, Uri uri, final int requiredSize)
        throws FileNotFoundException {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);

    int width_tmp = o.outWidth
            , height_tmp = o.outHeight;
    int scale = 1;

    while(true) {
        if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2); //This is the line that's getting the error
}

【问题讨论】:

    标签: performance android-imageview android-image android-memory


    【解决方案1】:

    直接在 imageview 中加载高分辨率图像可能会使您的 UI 滞后。为避免这种情况,要执行任何类型的调整大小,我建议使用 Picasso 或 Glide 图像加载库,以获得更好的图像加载性能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-04
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2010-12-21
      相关资源
      最近更新 更多