大家好:
我想在Android软件中实现上传图片的功能。Post上传,用httpclient实现 相关代码如下:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url); List<BasicNameValuePair> result = new ArrayList<BasicNameValuePair>();
//添加参数
BasicNameValuePair pair = new BasicNameValuePair("status", “文本信息”);
result.add(pair); httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost); 图片文件形式为:
File file = new File("/sdcard/picture.jpg"); 请问各位高手,如何将图片file转换并且添加到参数中?
谢谢
我想在Android软件中实现上传图片的功能。Post上传,用httpclient实现 相关代码如下:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url); List<BasicNameValuePair> result = new ArrayList<BasicNameValuePair>();
//添加参数
BasicNameValuePair pair = new BasicNameValuePair("status", “文本信息”);
result.add(pair); httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost); 图片文件形式为:
File file = new File("/sdcard/picture.jpg"); 请问各位高手,如何将图片file转换并且添加到参数中?
谢谢
解决方案 »
- @android:id/list 和@+id/android:list的区别。
- 为什么找不到getAllMessagesFromIcc
- webview load 图片问题
- android视频通话
- 如何在eclipse中查看android的消息队列
- copy帖子?大家看看说说怎么回事
- GridView显示重复数据
- android。如何查看/获取U盘设备的名称?
- 安卓onDraw重绘和invalidate()的问题,新手,各位帮个忙。
- android实现类似微信聊天界面,软键盘弹出时 标题不被挤上去
- 请教android培训学校
- 求助解決android4.2 make sdk 遇到的 error 45 的問題!
if (url == null || f == null) {
System.out.println("URL或者文件为NULL");
return false;
}
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
try {
FileEntity entity = new FileEntity(f, "binary/octet-stream");
httpPost.setEntity(entity);
response = client.execute(httpPost);
} catch (Exception e) {
System.out.println(e.toString());
} finally {
} // 判断上传的状态和打印调试信息
if (response != null
&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 打印调试信息,上传的url和上传的文件大小
System.out.println(MessageFormat.format(
"upload picture success! url = [{0}],file size = [{1}]", url,
f.length()));
return true;
}else
{
System.out.println(response.getStatusLine().getStatusCode());
}
return false;
}