我正在将一个iOS应用中的照片上传图像小部件移植到Android,并以multipart/form-data格式将数据作为HTTP POST发送。我需要在Android中复制这个功能,但遇到了一些问题。
现有的iOS代码片段:
相同的图像在两个平台上生成的字符串不同,结果是上传后服务器端无法呈现Android图像。
在Android / Java中,将图像转换为包含jpeg编码图像的字节数组的UIImageJPEGRepresentation等价物是什么?
现有的iOS代码片段:
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];
}
在安卓上我正在尝试:
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);
相同的图像在两个平台上生成的字符串不同,结果是上传后服务器端无法呈现Android图像。
在Android / Java中,将图像转换为包含jpeg编码图像的字节数组的UIImageJPEGRepresentation等价物是什么?