how to convert an image into base64 string

前端 未结 5 853
醉梦人生
醉梦人生 2020-12-19 04:09

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 encodin

相关标签:
5条回答
  • 2020-12-19 04:18

    Please use this code..

    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);
    

    Please import

    import android.util.Base64;
    
    0 讨论(0)
  • 2020-12-19 04:21

    try below code

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte b [] = baos.toByteArray();
    String base64String = Base64.encodeToString(b, Base64.DEFAULT);
    

    Dont forget to import android.util.Base64;

    0 讨论(0)
  • 2020-12-19 04:31
    // convert from bitmap to byte array
    public byte[] getBytesFromBitmap(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 70, stream);
        return stream.toByteArray();
    }
    
    // get the base 64 string
    String imgString = Base64.encodeToString(getBytesFromBitmap(someImg), 
                           Base64.NO_WRAP);
    
    0 讨论(0)
  • 2020-12-19 04:31

    maybe you can try

    String base64Result = Base64.encodeToString(yourByteArray[], Base64.DEFAULT);

    0 讨论(0)
  • 2020-12-19 04:33

    Method:

    private String bitmapToBase64(Bitmap bitmap) {
    
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream .toByteArray();
    
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
    

    Imports:

    import android.util.Base64;
    import java.io.ByteArrayOutputStream;
    
    0 讨论(0)
提交回复
热议问题