我怎么能用android发送多张图片

时间:2014-02-13 08:28:35

标签: java php android json multipartentity

你好我需要当我点击按钮“发送”我发送所有选中的图片(我在桌面图片中有所有这些图片[]

PHP中的

我这样做

<input id="uploadImageAct"  type="file" name="uploadImageAct[]"  data-max-size="2048"  accept="image/x-png, image/gif, image/jpeg" style="visibility: hidden" multiple="multiple">

并且在android中只有一张图片我这样做但我不知道我是怎么做多张图片的(我把所有图片都放在表格中[])

此解决方案适用于FileBody的一个图像,我需要在一个FileBody中使用多个图像

    public JSONObject post(String url, ArrayList<NameValuePair> nameValuePairs) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        for(int index=0; index < nameValuePairs.size(); index++) {
            if(nameValuePairs.get(index).getName().equalsIgnoreCase("uploadFile")) {
                // If the key equals to "image", we use FileBody to transfer the data
                entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
            } else {
                // Normal string data
             Charset chars = Charset.forName("UTF-8");
                entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue(),chars));
            }
        }

        httpPost.setEntity(entity);

        HttpResponse response = httpClient.execute(httpPost, localContext);


    HttpEntity httpEntity = response.getEntity();
    is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    json = sb.toString();
} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
    jObj = new JSONObject(json);
} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;
}

如何更改此功能以便我可以执行此操作

谢谢:)

1 个答案:

答案 0 :(得分:1)

您可以在一个请求中上传多个文件以及Android中的其他字符串参数。 为此,您必须在项目构建路径apache-mime4j-0.6.jarhttpmime-4.0.1.jar中包含2个库。

private void doFileUpload(){

    File file1 = new File(selectedPath1);
    File file2 = new File(selectedPath2);
    String urlString = "Your server location";
    try
    {
         HttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(urlString);
         FileBody bin1 = new FileBody(file1);
         FileBody bin2 = new FileBody(file2);
         MultipartEntity reqEntity = new MultipartEntity();
         reqEntity.addPart("uploadedfile1", bin1);
         reqEntity.addPart("uploadedfile2", bin2);
         reqEntity.addPart("user", new StringBody("User"));
         post.setEntity(reqEntity);
         HttpResponse response = client.execute(post);
         resEntity = response.getEntity();
         final String response_str = EntityUtils.toString(resEntity);
         if (resEntity != null) {
             Log.i("RESPONSE",response_str);
             runOnUiThread(new Runnable(){
                    public void run() {
                         try {
                            res.setTextColor(Color.GREEN);
                            res.setText("n Response from server : n " + response_str);
                            Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                       }
                });
         }
    }

或者只是访问CoderzHeaven,它可能对您有帮助。