【问题标题】:Encoding images for URI use为 URI 使用编码图像
【发布时间】:2014-11-30 00:11:01
【问题描述】:

我正忙于一个小的安卓应用程序。该应用程序使用 restful 连接到 WCF,而后者又连接到数据库。该应用程序确实有效。现在我想将图像发送到数据库。

[OperationContract]
[WebGet(UriTemplate = "sendImage/{seq}", ResponseFormat = WebMessageFormat.Json)]
string sendImage(string seq);

在 android 应用程序中,我使用 BASE64 对图像进行编码。

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray(); 
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

TextView textView = (TextView) findViewById(R.id.defaults);
textView.setText(encodedImage);

这种加密确实有效。问题是我现在将字符串encodedImage 发送到WCF 以写入数据库。因此,字符串在 URI 中传递。

"htp://_____.__/serv/ManagementSystem.svc/sendImage/" + encodedImage

问题是我从编码中得到的字符串包含/ 字符。

/9J/DASDFkFASDF/.....

这会导致 URI 出现问题,因为它会将 / 作为参数更改,因此会发现它不是一个有效的请求。有没有办法在不影响解码的情况下删除/,或者有没有更好的方法将图像转换为字符串并返回图像?

【问题讨论】:

    标签: android encoding base64 decoding


    【解决方案1】:

    如果你可以控制服务器端,我至少可以想到两个选择:

    1)base64编码后,将所有/替换为_,对url有效,没有特别的预赋值意义,base64不使用。在服务器端,您应用相反的替换;

    2) 这比选项 1 更直接,而不是 get,而是使用 put 或 post。

    【讨论】:

    • 谢谢,我没想过做选项二。开销比选项一少。
    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 2014-09-22
    • 1970-01-01
    • 2016-12-28
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多