How can I convert an image into a Base64 string?

前端 未结 14 1361
离开以前
离开以前 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:16

    This code runs perfect in my project:

    profile_image.buildDrawingCache();
    Bitmap bmap = profile_image.getDrawingCache();
    String encodedImageData = getEncoded64ImageStringFromBitmap(bmap);
    
    
    public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 70, stream);
        byte[] byteFormat = stream.toByteArray();
    
        // Get the Base64 string
        String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
    
        return imgString;
    }
    

提交回复
热议问题