大家好:
     我想在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转换并且添加到参数中?
   谢谢

解决方案 »

  1.   

    图片是用post上传数据流,直接给你代码看吧public boolean postFile(String url, File f) {
    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;
    }