6

I have a photo upload image widget in an iOS app that I am porting to Android. The data is sent as a HTTP POST in multipart/form-data format. I need to replicate this in Android but am having some problems.

Existing iOS code snippet:

if ([[dictionary objectForKey:key] isKindOfClass:[UIImage class]]) {
                        NSData *imageJPEG = UIImageJPEGRepresentation([dictionary objectForKey:key], 1);
                        NSString *filename = [NSString stringWithFormat:@"%@.jpg", key];
                        [(ASIFormDataRequest *)request setData:imageJPEG withFileName:filename andContentType:@"image/jpeg" forKey:key];
                    }

On Android I'm trying:

Bitmap fullImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fullImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

The resulting String is different for the same image on both platforms however, the result being that the Android image isn't rendering on the server side after being uploaded.

What is the Android/Java Equivalent to UIImageJPEGRepresentation for converting an image to a byte array containing a jpeg-encoded image?

David Berry
  • 40,941
  • 12
  • 84
  • 95
Sodman
  • 659
  • 5
  • 21
  • 1
    Don't expect JPEG representations to be reproducible, there's quite a bit of variability even with identical compression rates. The other difference I note is that you're sending the data as raw bytes in the iOS case, but sending it Base 64 encoded in the Android case. Are you setting the Content-Encoding header appropriately on Android? – David Berry Apr 18 '14 at 18:49
  • I'm sending a Content-Type: image/jpeg header, but I'm currently not setting Content-Transfer-Encoding. The team running the web-service we are uploading to has confirmed that they are receiving the image string and storing it in the database on their end. – Sodman Apr 18 '14 at 19:01
  • 1
    But without the encoding type being set, they're probably not decoding the base-64 back into a valid jpg image. – David Berry Apr 18 '14 at 19:31
  • @Shane I do not understand why you would base64 encode the image using Android, but not using iOs, since imho UIImageJPEGREpresentation does not return a base64 encoded value? – Klaus Oct 20 '14 at 07:50
  • I also want to do the same thing with MultipartEntityBuilder and stuck here. After you set the encodedImage string, how would u add it to the httpPost as multipartEntityBuilder.addPart(String xxx, ContentBody body). I would only upload the file but there are no images seen in the server... – Antoine Murion Apr 27 '15 at 03:59

1 Answers1

3

Make sure you're setting the Content-Encoding HTTP header to base64 so the server knows to properly decode the base 64 back into a valid jpg.

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • I have tried both "Content-Transfer-Encoding: base64" and "Content-Encoding: base64" but neither worked. JPEG file uploads successfully but format is not recognized. I am appending @Shane 's `encodedImage` by `request.append(encodedImage);` where `request` is of type `OutputStreamWriter`. Any suggestions would be appreciated – rockhammer Feb 16 '17 at 16:23