java - 应用程序引擎+端点生成字符串而不是字节数组作为方法参数

标签 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)。

这是一个错误还是有意为之?如果有意的话,我应该如何将图像传输到字符串?

谢谢!

最佳答案

好吧,我明白了。事实证明,它需要 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/

相关文章:

android - 堆转储中的奇怪内容

java - 自定义警报对话框内的 ListView 中的空对象引用

c# - 用蓝牙连接PC和安卓手机(c#)

mysql - 无法显示云sql数据库的内容

python - 在 Google AppEngine 中将数据导出为 XML 文件

Java 通用行映射器

JavaFX Button的disableProperty绑定(bind)到多个BooleanBindings

java - xmlrpc vs json/rest on java google appengine with android client

java - 使用linux shell中的可执行文件从java中的maven项目读取.txt文件

java - JSP多选连续选择