上传图像字节数组HttpURLConnection类和android [英] Uploading Image byte array with httpurlconnection and android

查看:164
本文介绍了上传图像字节数组HttpURLConnection类和android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发中,我想从我的Andr​​oid设备上传图片到我的服务器小android应用。我使用 HttpURLConnection类

I am developing small android application in which I wanted to upload image from my android device to my server. I am using HttpURLConnection for that.

我在下面的方式这样做:

I am doing this in following way:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow_down_float);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);

byte[] data = bos.toByteArray();

connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "image/jpeg");
connection.setRequestMethod(method.toString());

ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
bout.write(data); 
bout.close();

我使用 ByteArrayOutputStream ,但我不知道如何通过这些数据与我的HttpURLConnection类。这是通过原始图像数据的正确方法。我只是想发送字节数组,它包含的图像数据。无需改装或无多部分发送。
我的code没有任何错误做工精细,但它在我的服务器给我回复
{错误:不支持的MIME类型:inode的\\ / X-空}

I am using ByteArrayOutputStream but I don't know how to pass that data with my httpurlconnection. Is this the correct way to pass raw image data. I just wanted to send byte array which contains image data. No conversion or no multipart sending. My code working fine without any error but it my server gives me reply {"error":"Mimetype not supported: inode\/x-empty"}

我这样做是使用 setEntity 及其工作精细与HttpClient的。但是,我想用URLConnection的。

I did this with httpclient using setEntity and its working fine with that. But I want to use urlconnection.

我是不是做错了什么?如何做到这一点?
谢谢你。

Am I doing something wrong? How to do this? Thank you.

推荐答案

您必须打开输出流连接,并在其中写入数据。
你可以试试这个:

You must open the output stream connection and write the data in it. You could try this:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow_down_float);

connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "image/jpeg");
connection.setRequestMethod(method.toString());
OutputStream outputStream = connection.getOutputStream();

ByteArrayOutputStream bos = new ByteArrayOutputStream(outputStream);
bitmap.compress(CompressFormat.JPEG, 100, bos);

bout.close();
outputStream.close();

通过下面的语句:

bitmap.compress(CompressFormat.JPEG, 100, bos);

您正在做两件事情:。com preSS位图,并发送所得数据BOS流的结果数据发送到输出流连接(即建立JPG字节),

You are doing two things: compress the bitmap and send the resulted data (the bytes that build the jpg) to bos stream, that send the resulted data to the output stream connection.

您也可以直接写连接的输出流中的数据,要更换此:

Also you can write the data in the output stream of the connection directly, replacing this:

ByteArrayOutputStream bos = new ByteArrayOutputStream(outputStream);
bitmap.compress(CompressFormat.JPEG, 100, bos);

这一点:

bitmap.compress(CompressFormat.JPEG, 100, outputStream);

我希望这有助于你理解HttpURLConnection类是如何工作的。

I hope this helps you understand how HttpUrlConnection works.

此外,你应该不完全加载整个位图避免内存不足异常,打开与流的位图,例如。

Also, you should not load the whole bitmap entirely for avoid the "out of memory" exceptions, opening the bitmap with streams, for example.

这篇关于上传图像字节数组HttpURLConnection类和android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆