在Android应用程序使用的Base64图像编码和解码 [英] Image encoding and Decoding using Base64 in android application

查看:208
本文介绍了在Android应用程序使用的Base64图像编码和解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序面临的编码和解码图像的小问题字符串,并将其传递到Web服务。        得到位图图像后,我把它转换成字节[]和连接code字符串值,但在某些情况下,它会显示错误,我不知道为什么说到。        还有一疑问,是的Base64类只支持位图图像转换为字符串或可用做同样的任何其他工具。

In my application I have faced a small issue in encoding and decoding images to String and pass it to web service. After getting the bitmap image, I convert it into byte[] and encode to String value but in Some cases it shows error I don't know why it comes. And one more doubt is Base64 class only supports to convert Bitmap image to String or any other tools available to do the same.

在此先感谢...

推荐答案

在OutOfMemoryError异常的情况下,低于code可以帮助我。

In the case of OutOfMemoryError, below code helps me.

public String BitMapToString(Bitmap bitmap){
        ByteArrayOutputStream baos=new  ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100, baos);
        byte [] b=baos.toByteArray();
        String temp=null;
        try{
        System.gc();
        temp=Base64.encodeToString(b, Base64.DEFAULT);
        }catch(Exception e){
            e.printStackTrace();
        }catch(OutOfMemoryError e){
            baos=new  ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG,50, baos);
            b=baos.toByteArray();
            temp=Base64.encodeToString(b, Base64.DEFAULT);
            Log.e("EWN", "Out of memory error catched");
        }
        return temp;
  }

基本上,我所做的是:我赶上OutOfMemoryError异常,并在catch块我调整其大小的50%,然后我EN code到字符串

Basically what i did is : i catch OutofMemoryError and in that catch block i resize it by 50% and then i encode it to string.

这篇关于在Android应用程序使用的Base64图像编码和解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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