且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android应用程序使用的Base64图像编码和解码

更新时间:2023-02-11 12:16:01

在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.