将位图转换为Base64字符串会导致OutOfMemory错误 [英] Converting bitmap to Base64 string causes OutOfMemory error

查看:310
本文介绍了将位图转换为Base64字符串会导致OutOfMemory错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下方法将位图转换为Base64编码的字符串,但是对于很大的图像,我遇到了OutOfMemory错误:

I'm currently using the following method to convert a bitmap into a Base64 encoded string, however for very big images I'm getting an OutOfMemory error:

public static String convertBitmapToBase64String(Bitmap bmp) throws OutOfMemoryError
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] byteArrayImage = baos.toByteArray();

    String base64EncodedImg = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
    return base64EncodedImg;
}

我看到了这个答案,但是它使用了IOUtils: https://stackoverflow.com/a/24877734/720175

I saw this answer, however it uses IOUtils: https://stackoverflow.com/a/24877734/720175

是否可以仅使用通常包含的标准库将大位图转换为base64编码的字符串?

Is it possible to do convert a large bitmap into a base64 encoded string using only standard libraries normally included?

推荐答案

bmp.compress(Bitmap.CompressFormat.JPEG, (AS LOWER), baos);
bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos);

您可以降低质量水平,然后再正常工作

You can reduce the quality level then its working fine

这篇关于将位图转换为Base64字符串会导致OutOfMemory错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆