简体   繁体   中英

Base 64 Encoding issue (MIME-NO-LINEFEEDS)

On my Android App, I'm trying to encode an image to a Base64 String to send it to an API that handles it. It works correctly when encoding the file from AngularJS (web) and sending it, but still not finding a way to make it work with Android .

Here is my code:

 ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
 if(image.compress(compressFormat, quality, byteArrayOS)) {
     return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.NO_WRAP); // tried several flags ...
  } else {
     return null;
  }

where:

compressFormat = Bitmap.CompressFormat.JPEG;
quality = 100;
// The variable image is a Bitmap Image

And the error message :

Could not read document: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '\\"' (code 0x22) in base64 content\\n at [Source: java.io.PushbackInputStream@5d1a0f8e; line: 1, column: 276951]

Update : There are some images (strings) that are accepted and others no. But I can't figure out why ...

not accepted String : https://jsfiddle.net/dqpth401/

accepted String : https://jsfiddle.net/r8gLejsL/2/

I've been stuck with this issue and any hint/advice would be welcome !

Thanks

The error mentions a double quote ( " ) at what is probably the end. The data is correct and convertable to a smiling face.

Now that suggests that something else is wrong:

  • Content-Length, minus-padding at the end, plus-to-space http conversion probably work (other data)
  • Hence remains the component: HTML image tag or whatever is used.

In the tag there is some meta info text followed by base64

src="data:image/jpeg;base64, ..."

Very speculative: a missing e in jpeg (correct) as opposed to image/jpg (incorrect).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM