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

Sub*_*ddy 8 xml base64 encoding android encode

我想将图像转换为base 64 encode to string.从那里发送到oma_status-iconxml格式的服务器.

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

有没有其他方法将图像转换为base64字符串?

plz..help ...

我的代码是:

        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);
Run Code Online (Sandbox Code Playgroud)

Nik*_*hil 15

请使用此代码..

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);
Run Code Online (Sandbox Code Playgroud)

请导入

import android.util.Base64;
Run Code Online (Sandbox Code Playgroud)

  • Base64.DEFAULT: - 传递DEFAULT导致符合RFC 2045的输出. (5认同)
  • base64.DEFAULT而不是samll base64使用了Base64.DEFAULT (3认同)