安卓:发送通过张贴图片 [英] Android: Sending an image through POST
本文介绍了安卓:发送通过张贴图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在寻找周围的解决方案,以及所遇到的多部分和不同的设置,但我似乎无法得到它的正常工作。
下面是我到目前为止所。
编辑:服务器端错误我得到一个500。我认为,这是因为我送的数据是太大了,一个请求或者是格式不正确
。 ByteArrayOutputStream宝=新ByteArrayOutputStream();
bm.com preSS(Bitmap.Com pressFormat.JPEG,90,宝);
byte []的BA = bao.toByteArray();
字符串BA1 = Base64.en codeToString(BA,Base64.URL_SAFE);
mParams.add(新BasicNameValuePair(故事[图]BA1));
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(的mpath);
//添加数据
尝试
{
httppost.setHeader(授权,Base64.en codeToString(新的StringBuilder(SSSSS).append(:)追加(SSSSSS)的ToString()的GetBytes(UTF-8),Base64.URL_SAFE | Base64.NO_WRAP));
httppost.setEntity(新UrlEn codedFormEntity(mParams));
HTT presponse RH = httpclient.execute(httppost);
Log.v(TAG的回应:+ rH.toString());
INT F = 0;
}
赶上(Htt的presponseException E)
{
Log.e(TAG,e.getLocalizedMessage());
Log.e(TAG,e.getMessage());
e.printStackTrace();
}
解决方案
这是我昨天做了,也许这将有助于
位图bitmapOrg = images.get(0);
ByteArrayOutputStream宝=新ByteArrayOutputStream();
字符串UPLOAD_URL = prepare_upload_url();
bitmapOrg.com preSS(Bitmap.Com pressFormat.JPEG,90,宝);
byte []的数据= bao.toByteArray();
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost postRequest =新HttpPost(UPLOAD_URL);
MultipartEntity实体=新MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
//设置数据和图像内容类型头
entity.addPart(文件,
新ByteArrayBody(数据,图像/ JPEG,文件));
postRequest.setEntity(实体);
尝试 {
HTT presponse响应= httpClient.execute(postRequest);
//读取响应
串jsonString = EntityUtils.toString(response.getEntity());
Log.v(ProgramConstants.TAG,上传文件后,
+ jsonString);
}赶上(ClientProtocolException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
I've been searching around for the solution, and have come across the multipart and different setups, but I can't seem to get it working correctly.
Here's what I have so far.
Edit: The server side error I'm getting is a 500. I assume that's because the data I'm sending is either too big for one request or is in an incorrect format.
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.URL_SAFE);
mParams.add(new BasicNameValuePair("story[image]",ba1));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mPath);
// Add your data
try
{
httppost.setHeader("Authorization", Base64.encodeToString(new StringBuilder(sssss).append(":").append(ssssss).toString().getBytes("UTF-8"), Base64.URL_SAFE|Base64.NO_WRAP));
httppost.setEntity(new UrlEncodedFormEntity(mParams));
HttpResponse rH = httpclient.execute(httppost);
Log.v(TAG, "response: " + rH.toString());
int f = 0;
}
catch(HttpResponseException e)
{
Log.e(TAG, e.getLocalizedMessage());
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
解决方案
This is what I did yesterday, maybe it will help
Bitmap bitmapOrg = images.get(0);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
String upload_url = prepare_upload_url();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] data = bao.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(upload_url);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
//Set Data and Content-type header for the image
entity.addPart("file",
new ByteArrayBody(data, "image/jpeg", "file"));
postRequest.setEntity(entity);
try {
HttpResponse response = httpClient.execute(postRequest);
//Read the response
String jsonString = EntityUtils.toString(response.getEntity());
Log.v(ProgramConstants.TAG, "after uploading file "
+ jsonString);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这篇关于安卓:发送通过张贴图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文