How can I convert an image into a Base64 string?

前端 未结 14 1202
离开以前
离开以前 2020-11-22 06:52

What is the code to transform an image (maximum of 200 KB) into a Base64 String?

I need to know how to do it with Android, because I have to add the functionali

14条回答
  •  遇见更好的自我
    2020-11-22 07:14

    Convert an image to Base64 string in Android:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    

提交回复
热议问题