如何将图像转换为base64字符串

人气:730 发布:2022-10-16 标签: xml android encode encoding base64

问题描述

我想将图像转换为 base 64 编码为字符串.从那里以 oma_status-icon xml 格式发送到服务器.

I want to convert image to base 64 encode to string. from that to send to server with oma_status-icon xml format.

但是我从服务器响应中得到了不受支持的编码......

but I am getting unsupported encoding from the server response....

还有其他方法可以将图片转base64字符串吗??

is there any other way to convert image to base64 string??

请..帮助...

我的代码是:

        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  R.drawable.image);

        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
        byte [] ba = bao.toByteArray();

         String ba1=Base64.encodeBytes(ba);

推荐答案

请使用此代码..

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

请导入

import android.util.Base64;

877