首页 > 解决方案 > 如何使用 asyncTask 将以 base64 格式选择的多个图像发送到服务器?

问题描述

我正在尝试将带有 exif 数据的 base64 格式的多个图像发送到服务器。仅发送图像时我没有问题,并且一切正常,但是当我尝试发送多张图像时,无论选择多少张图像,最多只能发送两张。没有错误,但我找不到我的错误。任何帮助都将是有用的提前感谢。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_CANCELED) {
        return;
    }

    if (requestCode == GALLERY) {
        if (data != null) {
                // Get the Image from data

                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                imagesEncodedList = new ArrayList<String>();
                if (data.getData() != null) {

                    //this section gets the single image and works fine

                } else {
                    if (data.getClipData() != null) {
                        ClipData mClipData = data.getClipData();
                        ArrayList<Uri> mArrayUri = new ArrayList<Uri>();

                        for (int i = 0; i < mClipData.getItemCount(); i++) {
                            ClipData.Item item = mClipData.getItemAt(i);
                            uri = item.getUri();
                            mArrayUri.add(uri);
                            // Get the cursor
                            Cursor cursor = getActivity().getContentResolver().query(uri, filePathColumn, null, null, null);
                            String path = null;

                            // SDK >= 11 && SDK < 19
                            if (Build.VERSION.SDK_INT < 19)
                                path = RealPathUtils.getRealPathFromURI_API11to18(getContext(), uri);

                                // SDK > 19 (Android 4.4)
                            else
                                path = RealPathUtils.getRealPathFromURI_API19(getContext(), uri);
                            Log.d(TAG, "File Path: " + path);
                            // Get the file instance
                            File file = new File(path);

                            // Move to first row
                            cursor.moveToFirst();

                            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                            imageEncoded = cursor.getString(columnIndex);
                            imagesEncodedList.add(imageEncoded);
                            cursor.close();

                            list.add(file.toString());

                            getExifData(path);

                            new SendImage().execute();

                        }
                        Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());
                    }
                }
                // Uri uri = data.getParcelableExtra("path");

        }

    } else if (requestCode == CAMERA) {

        if (data != null) {

            //also this section works good
        }



    }

}

发送图像()

public class SendImage extends AsyncTask<String, Void, String> {

    protected void onPreExecute() {
      //  showpDialog();
    }

    protected String doInBackground(String... arg0) {

        try {

            URL url = new URL("");

            JSONObject postDataParams = new JSONObject();
            postDataParams.put("user_id", user_id);
            postDataParams.put("event_id", event_id);
            postDataParams.put("img_loction_l", img_location_l);
            postDataParams.put("img_loction_r", img_location_r);
            postDataParams.put("images_file", ConvertImage);
            postDataParams.put("img_time", img_time);
            Log.e("params", postDataParams.toString());

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));

            writer.flush();
            writer.close();
            os.close();

            int responseCode = conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {

                BufferedReader in = new BufferedReader(new
                        InputStreamReader(
                        conn.getInputStream()));

                StringBuffer sb = new StringBuffer();
                String line = "";

                while ((line = in.readLine()) != null) {

                    sb.append(line);
                    break;
                }

                in.close();
                return sb.toString();

            } else {
                return new String("false : " + responseCode);
            }
        } catch (Exception e) {
            return new String("Exception: " + e.getMessage());
        }

    }

    @Override
    protected void onPostExecute(String result) {
      //  hidepDialog();
        try {
            JSONObject jsonObject = new JSONObject(result);
            String msg = jsonObject.getString("msg");//Your image has been Uploaded
            Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();

        } catch (JSONException e) {
            e.printStackTrace();
        }
   //     if (img_marker != null) {
    //        img_marker.remove();
   //     }
        img_location_r = null;
        img_location_l = null;
        ConvertImage = null;
        img_time = null;
        img_gallery.setImageDrawable(null);
    }
}

public String getPostDataString(JSONObject params) throws Exception {

    StringBuilder result = new StringBuilder();
    boolean first = true;

    Iterator<String> itr = params.keys();

    while (itr.hasNext()) {

        String key = itr.next();
        Object value = params.get(key);

        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(key, "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(value.toString(), "UTF-8"));
    }

    return result.toString();
}

标签: androidjsonimagebase64

解决方案


推荐阅读