App引擎+端点生成字符串而不是字节数组作为方法参数

时间:2014-08-30 22:51:30

标签: java android google-app-engine google-cloud-datastore google-cloud-endpoints

我在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)。

这是一个错误还是打算?如果打算,我该如何将我的图像转换为字符串?

谢谢!

1 个答案:

答案 0 :(得分:5)

好吧,我明白了。事实证明它期望base64格式的字节数组,这解释了为什么byte []签名变为String。

所以在Android中从byte []转到我使用的base64字符串,其中mPicture是我的字节数组:

Base64.encodeToString(mPicture, Base64.DEFAULT);

接收一个String并转换回byte [],其中picture是从端点接收的base64字符串:

Base64.decode(picture, Base64.DEFAULT);

希望这有帮助!