首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android中压缩图片

如何在Android中压缩图片
EN

Stack Overflow用户
提问于 2015-08-11 15:52:28
回答 2查看 57关注 0票数 0

我想在上传到服务器之前压缩图片,压缩后它将小于150kb,我已经做了here,问题是当图片大小超过6M,它发生了oom。我怎样才能避免oom!

EN

回答 2

Stack Overflow用户

发布于 2015-08-11 16:27:01

您链接的代码没有正确处理位图,您必须回收它们。当Android系统处理位图时,它会正确地回收它们,但无论何时您自己处理它,您都必须正确地将资源返回给系统。

请仔细阅读此https://developer.android.com/training/displaying-bitmaps/manage-memory.html,并应用所解释的观点

票数 0
EN

Stack Overflow用户

发布于 2015-08-11 17:37:40

参数ImageFile,你想要调整大小,高度和宽度的图像,你想在输出。

public static Bitmap Shrinkmethod(字符串文件,int width,int height) {

代码语言:javascript
运行
复制
    BitmapFactory.Options bitopt = new BitmapFactory.Options();
    bitopt.inJustDecodeBounds = true;
    Bitmap bit = BitmapFactory.decodeFile(file, bitopt);

    int h = (int) Math.ceil(bitopt.outHeight / (float) height);
    int w = (int) Math.ceil(bitopt.outWidth / (float) width);

    if (h > 1 || w > 1) {
        if (h > w) {
            bitopt.inSampleSize = h;

        } else {
            bitopt.inSampleSize = w;
        }
    }
    bitopt.inJustDecodeBounds = false;
    bit = BitmapFactory.decodeFile(file, bitopt);
    return bit;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31936129

复制
相关文章

相似问题

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