Pixeldroid Modding
Pixeldroid Modding

Reputation: 45

Android Image to Base64

I want to convert my app images to base64 so it won't show on the gallery. I have tried various techniques. The image is from a zip file so it is a byte array at that point. The length of the base64 change when I change the byte array size. What is the proper byte array size? And the base64 encoded image doesn't work too. The primary code is String encodedImage = Base64.encodeToString(buffer, Base64.DEFAULT); buffer is the byte array(102400) and it contains the image too. The image is a 7KB file and the output is 400KB

Upvotes: 0

Views: 1273

Answers (1)

Harshil
Harshil

Reputation: 1000

You may try following function to convert image into Base64:

public void toStringImage(Bitmap bmp) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
}

Above function takes Bitmap image and converts it into Base64 encoded string. This is working in my project and I hope this will help you too.

Upvotes: 2

Related Questions