0

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);
        }
jampez77
  • 5,012
  • 7
  • 32
  • 52
  • `The app decodes base64 to PNG `. You should start showing us that code. And also you did not tell where that png was saved. Did you save it as a file? – greenapps Nov 18 '16 at 12:25

3 Answers3

3

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

Ashwin
  • 7,277
  • 1
  • 48
  • 70
1

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;
}
Sahil Garg
  • 263
  • 1
  • 20
1

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.

greenapps
  • 11,154
  • 2
  • 16
  • 19