【问题标题】:how to send image through android REST Web services and save it and retrieve from mysql database如何通过android REST Web服务发送图像并将其保存并从mysql数据库中检索
【发布时间】:2014-07-29 10:40:01
【问题描述】:

我是 Android 开发新手,我只是想知道如何通过 android REST Web 服务发送图像并将其保存并从 MySQL 数据库中检索

【问题讨论】:

  • 你需要 Base64 java 类文件。

标签: java android json


【解决方案1】:

我的经验,客户端将image-string(编码的图像字节数组)发送到服务器并将其保存到数据库中。 为了显示图像,客户端从服务器获取image-string 并将image-string 解码为字节数组。尝试显示image byte array

public static String encodeImage(byte[] imageByteArray) {
    return Base64.encodeBase64URLSafeString(imageByteArray);
}

public static byte[] decodeImage(String imageDataString) {
    return Base64.decodeBase64(imageDataString);
}

使用org.apache.commons.codec.binary.Base64

【讨论】:

  • 你能告诉我发送到网络服务和存储在数据库中的完整代码吗?
  • @Ajay,如果你可以将图片转换为encoded string,就像将Hello World字符串发送到服务器。
  • 从图像中检索后如何解码和显示图像?
  • 是的。你能告诉我用于存储图像的数据类型吗?我应该使用 blob 还是 VARCHAR(MAX)。?
  • @Ajay,我怎么知道你的图片大小?这就是为什么我不能评论数据库的数据类型。这将是另一个话题。
【解决方案2】:

您可以将位图图像编码为 Base64,因此您将拥有一个编码为字符串的图像。您可以使用 REST API 将该字符串发送并保存到数据库。稍后当您要显示它时,再次下载并解码为 Bitmap。 Here is how you can do encoding/decoding.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    相关资源
    最近更新 更多