【问题标题】:android - web api sending/receiving an imageandroid - web api 发送/接收图像
【发布时间】:2015-08-16 00:23:12
【问题描述】:

我确定这是一个常见问题,我可能会找到解决方案,但我不明白。此外,我这样做完全是盲目的。还有一点是:我不想使用第三方库。

我需要通过 c# rest webservice 将图像从我的 Android 应用程序发送到我的服务器。

我看了这个方法将位图转换为字节[]。

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 0, outputStream);       
        return outputStream.toByteArray();
    }

这里我有两个(至少)问题:

  1. 如何以 JSON 格式发送?我尝试了带有 COMMON 和 URLSAFE(或类似的)标志的 Base64.encode(),但在服务器端出现错误:不是有效的 Base64。

  2. 那我想客户端是没问题的,那我该如何接收和处理byte[]呢?现在它似乎尝试自动转换并且它失败了,可能是因为客户端发送了无效数据或者......我不知道。

我现在无法提供代码(事实上我认为我根本没有代码来执行此操作),但如果需要,我会更新我所拥有的。

感谢并为这个可怕的问题感到抱歉

【问题讨论】:

    标签: c# android json rest asp.net-web-api


    【解决方案1】:

    安卓:

    byte[] content = getBitmapAsByteArray(bitmap);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new ByteArrayEntity(content));           
    HttpResponse response = httpClient.execute(httpPost);
    

    c#.net

    byte[] fileData = null;
    using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
    {
        fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
    }
    

    【讨论】:

    • 首先,感谢您的回答。其次,Android 部分已全部弃用。
    猜你喜欢
    • 2015-01-08
    • 1970-01-01
    • 2012-01-02
    • 2019-08-03
    • 2014-07-17
    • 2018-04-27
    • 2018-02-14
    • 2011-07-01
    • 1970-01-01
    相关资源
    最近更新 更多