Base64 Android 编码到 PHP 解码出错
我是一个法国人,所以,我很抱歉我的英语...
我正在开发一个与 PHP REST 服务通信的 Android 应用程序。因此,当我尝试将图像文件编码为 Base64 时,如下所示:
InputStream fileInputStream = context.getContentResolver().openInputStream(uri);
BufferedInputStream in = new BufferedInputStream(fileInputStream);
StringWriter out = new StringWriter();
int b;
while ((b = in.read()) != -1)
out.write(b);
out.flush();
out.close();
in.close();
String encoded = new String(android.util.Base64.encode(out.toString()
.getBytes(), android.util.Base64.DEFAULT));
在服务器端,我做出:
$data=base64_decode(chunk_split($base64BinaryData));
结果是我的图像文件已损坏!
信息:图像是由模拟器模式下的 android.provider.MediaStore.ACTION_IMAGE_CAPTURE Activity 的 Intent 制作的(avd 5554)
我已经阅读了很多有关类似问题的讨论,但没有解决我的错误。
感谢您的帮助
问候,
I'm a french guy, so, I'm sorry for my english...
I'm developing an Android App which communicate with a PHP REST service. So, when I try to encode an image file into Base64 like this :
InputStream fileInputStream = context.getContentResolver().openInputStream(uri);
BufferedInputStream in = new BufferedInputStream(fileInputStream);
StringWriter out = new StringWriter();
int b;
while ((b = in.read()) != -1)
out.write(b);
out.flush();
out.close();
in.close();
String encoded = new String(android.util.Base64.encode(out.toString()
.getBytes(), android.util.Base64.DEFAULT));
On server side, I make :
$data=base64_decode(chunk_split($base64BinaryData));
The result is that my image file is corrupted!
INFO : the image is made by an Intent to android.provider.MediaStore.ACTION_IMAGE_CAPTURE Activity in Emulator mode (avd 5554)
I've already read lots of discussions about similar problem but nothing fix my bug.
thanks for help
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设:
是错误的。 “出”是什么类型?如果那是图像,那么您需要获取图像的原始字节,而不是对象的字符串表示形式的字节。
I'm assuming that:
is wrong. What type is the "out"? If that's an image, then you need to get the raw bytes of the image, not the bytes of the string representation of the object.
你可以删除 chunk_split 吗?
另外,你能散列这两个二进制文件吗?也许使用 sha1 看看它们是否真的不同?
Can you just drop the chunk_split?
Also, can you hash the two binaries? Perhaps using sha1 and see if they're actually different?