0

I need to convert an image choosen from the gallery into a base64 string. Then, I pass the baase64 string as a parameter for an API request. There is only one problem. When I use netbeans it works, when I use Android Studio it doesn't. I found that the problem is the base64 string output. I don't know why, if I use the same exactly image, the output is different. Maybe the problem happpens because I have to use the same exact method to read the image file...?

That's my code in Netbeans(working):

    InputStream inputStream = new FileInputStream("testImage.jpg");
    byte[] bytes;
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    bytes = output.toByteArray();
    String encodedFile = Base64.getEncoder().encodeToString(bytes);

And that's the code in Android Studio:

            Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageData = baos.toByteArray();
            InputStream inputStream = getContentResolver().openInputStream(imageUri);

            byte[] buffer_new = new byte[8192];
            int bytesRead;
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            try {
                while ((bytesRead = inputStream.read(buffer_new)) != -1) {
                    output.write(buffer_new, 0, bytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            bytes = output.toByteArray();
            String encodedImage = Base64.encodeToString(bytes,Base64.DEFAULT);
            Log.v("encodedImage", encodedImage);

The ouput string are almost the same only at the first char..then they are different..with the android studio encoded string, I get this error when I try to use the API.

BAD_ARGUMENTS:<key>
Error while parsing some arguments. This error may be caused by illegal type or length of argument.

What should I use to get the same base64 string? ps. in Netbeans the image is a file in the same folder of the project, in android studio the user can load a picture from gallery.

Nicola
  • 301
  • 3
  • 20
  • 1
    How do you know the images are the same? – President James K. Polk Dec 26 '18 at 01:56
  • 1
    You are not using the same method to perform the encoding in the two cases you present. You might start with that. – John Bollinger Dec 26 '18 at 02:28
  • 1
    For us to be able to help you, though, we need to know which `Base64` class(es) you are using. Evidently not `java.util.Base64`, because that class does not off *either* of those encoding methods. – John Bollinger Dec 26 '18 at 02:29
  • 1
    And this all matters because base-64 is not a single encoding; rather, it is an encoding *form*. There is a fairly wide variety of specific base-64 dialects in use, not reliably interoperable (and some not interoperable at all). `java.util.Base64` alone supports three different ones. – John Bollinger Dec 26 '18 at 02:32
  • 1
    What's all that Bitmap code, you are not using it at all! Just confusing! – lionscribe Dec 26 '18 at 04:36
  • @JohnBollinger with android I use import android.util.Base64; instead with netbeans I use import java.util.Base64;. I see that the same image, downloaded from google image, have 6kb of difference..anyway, the strange things is that if I get the base64 from android, and then coping the string into netbeans, then the API doesn't work. Maybe the base64 it's wrong? – Nicola Dec 26 '18 at 08:30
  • 1
    It would be a good idea to use the same Base64 implementation in both places. But there is an added wrinkle here: you are encoding to *Strings*. To transfer a String from one machine to another, there is an additional encoding / decoding involved (chars to bytes and back), which affords another opportunity for mismatch. If the length difference is as large as you say, then the two most likely scenarios I see are (1) you are performing one of the Bas64 encodings incorrectly (not just differently), or (2) you are transferring the resulting string incorrectly. – John Bollinger Dec 26 '18 at 15:46

0 Answers0