jampez77
jampez77

Reputation: 5241

Android encoding PNG to base64 string producing invalid results

The app decodes base64 to PNG but when I encode the file back to base64 in order to send to a server the resulting base64 is different and does not produce an image.

Here is the start of the original base64 string: /9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAYAAIdp

and here is the start of the base64 after it is encoded from and PNG file: iVBORw0KGgoAAAANSUhEUgAAD8AAAAvQCAIAAABPl1n3AAAAA3NCSVQICAjb4U/gAAAgAElEQVR4nO

This is the code I'm using to encode the file to base64:

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = true;
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inDither = false;

        File file = new File(root +"/saved_images/"+note.imageLocation );
        if(file.exists()){
            // TODO perform some logging or show user feedback
            try {
            Bitmap myBitmap = BitmapFactory.decodeFile(root +"/saved_images/"+note.imageLocation, options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();


            myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            JSONObject object =new JSONObject();
                object.put("image_type", "image/png");
                object.put("image_data", Base64.encodeToString(byteArray, Base64.DEFAULT));


                if (note.serverID == -1) {
                    toReturn.put(String.valueOf(i), object);
                }else{
                    toReturn.put(String.valueOf(note.serverID), object);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            i--;
        }else{
            Log.i("File Not Found", "NoteModel - "+file);
        }

Upvotes: 0

Views: 1597

Answers (3)

Ashwin
Ashwin

Reputation: 7667

Try my solution which explains how to encode bitmap to base64 and decode base64 back to bitmap successfully.

Upvotes: 3

greenapps
greenapps

Reputation: 11224

If you indeed received a base64 string and decoded it to png bytes which you saved to file without using an intermediate Bitmap then you should just load that png file in a byte buffer and encode that byte buffer to a base64 string which you will upload.

If you did use a Bitmap to save the image then that was a bad idea.

Do not use classes Bitmap and BitmapFactory to up and download files. You will end up with different images.

Upvotes: 1

Sahil Garg
Sahil Garg

Reputation: 262

Please try this code,

 public String encodeToBase64(Bitmap image) {
    Bitmap immagex = Bitmap.createScaledBitmap(image, 350, 350, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    Log.e("LOOK", imageEncoded);
    return "data:image/png;base64," + imageEncoded.replace(" ", "").replace("\n", "");
}

public Bitmap encodeToBitmap(String encodedImage) {
    encodedImage = encodedImage.substring(encodedImage.indexOf(",") + 1);
    byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    return bitmap;
}

Upvotes: 1

Related Questions