我有在 Android 客户端和应用引擎后端之间工作的 GAE + 端点。
我现在想要使用 JDO 将小图像存储为 Blob 数据类型。我的模型后端有以下两种方法:
public byte[] getPicture() {
if (picture == null) {
return null;
}
return picture.getBytes();
}
public void setPicture(byte[] bytes) {
this.picture = new Blob(bytes);
}
但是,当我为 Android 客户端生成端点时,setPicture(byte[] bytes) 方法签名将转换为 setPicture(String bytes)。
这是一个错误还是有意为之?如果有意的话,我应该如何将图像传输到字符串?
谢谢!
最佳答案
好吧,我明白了。事实证明,它需要 Base64 格式的字节数组,这解释了为什么 byte[] 签名更改为字符串。
因此,在 Android 中,我使用了从 byte[] 到 base64 字符串的方法,其中 mPicture 是我的字节数组:
Base64.encodeToString(mPicture, Base64.DEFAULT);
接收一个字符串并转换回 byte[],其中图片是从端点接收的 base64 字符串:
Base64.decode(picture, Base64.DEFAULT);
希望这有帮助!
关于java - 应用程序引擎+端点生成字符串而不是字节数组作为方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25587401/