首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调整图像大小时,安卓获取java.lang.OutOfMemoryError

调整图像大小时,安卓获取java.lang.OutOfMemoryError
EN

Stack Overflow用户
提问于 2017-11-30 06:03:21
回答 0查看 36关注 0票数 0

我有一个可以在我的手机上正常工作的。然而,当我检查实时崩溃数据时,人们得到了内存不足的错误。

java.lang.OutOfMemoryError

我试图从照片加载图像,并试图使图像更小。

这是我的代码

requiredSize为1000

代码语言:javascript
运行
复制
 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
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47562619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档