我是一个法国人,所以,我很抱歉我的英语…
我正在开发一个与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));
结果是我的图像文件损坏了!
INFO:该图像是由Intent生成的android.provider.MediaStore。ACTION_IMAGE_CAPTURE活动在模拟器模式(avd 5554)
我已经读了很多关于类似问题的讨论,但没有一个能解决我的错误。
thanks for help
认为,
我假设:
out.toString().getBytes()
是错误的。"out"是什么类型的?如果这是一个图像,那么您需要获得图像的原始字节,而不是对象的字符串表示的字节。
你能把chunk_split去掉吗?
另外,你能哈希这两个二进制文件吗?也许可以使用sha1,看看它们是否真的不同?