android - web api发送/接收图像

时间:2015-06-02 09:27:10

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

我确定这是一个常见问题,我可能会找到解决方法,但我并不了解它们。除此之外,我完全失明了。另一点是:我不想使用第三方库。

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

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

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. 然后我认为客户端没问题,那么我如何设法接收和处理字节[]?现在它似乎尝试自动转换并且失败,可能是因为客户端发送了无效数据或者......我不知道。

  3. 我现在无法提供代码(实际上我认为我根本没有代码可以执行此操作),但如果需要,我会更新我的代码。

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

1 个答案:

答案 0 :(得分:0)

Android:

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);
}
相关问题