简体   繁体   中英

How to upload multiple image from gallery in android?

I want to upload multiple image from gallery to server but getting only one image not multiple.

following is the code for it

FirstFragment.java

private void orderRequest() {

    final OrderRequestModel model = basicInfiFragment.getData();
    model.setSs(steelFragment.getProductInfo());
    model.setAluminium(aluminiumFragment.getProductInfo());

    SimpleMultiPartRequest orderRequest = new SimpleMultiPartRequest(Request.Method.POST,
            Constance.baseURL + Constance.orderURL, new com.android.volley.Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            DialogUtil.hideProgrss();
            ProductModel mResponse = new Gson().fromJson(response, ProductModel.class);
            if (mResponse.getStatus().equalsIgnoreCase(Constance.success)) {
                Toast.makeText(getContext(), mResponse.getMessage(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), MainActivity.class);
                getContext().startActivity(intent);
                getActivity().finish();
            } else {
                SnackUtil.mackText(mBinding.layoutRoot, mResponse.getMessage(), true);
            }
            L.e(response);
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            L.e(error.toString());
            DialogUtil.hideProgrss();
            DialogUtil.someThingWentWrong(getContext());
        }
    });

    Map<String, String> headerMap = new HashMap<>();
    headerMap.put("token", SP.getString(SP.TOKEN));



    for (ImageDetails imageDetails : model.getImageList()) {

            orderRequest.addStringParam("json", new Gson().toJson(model));
            orderRequest.addFile("siteImages",imageDetails.getPath());
            orderRequest.addMultipartParam(imageDetails.getName(), getActivity().
                    getContentResolver().getType(imageDetails.getURI()), imageDetails.getPath());
            orderRequest.setHeaders(headerMap);
        }


    DialogUtil.showProgress(getContext());
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    requestQueue.add(orderRequest);
}

i tried a lot but it is uploading only one image please help me out of these getting stuck since last three days..

SecondFragment.java

public OrderRequestModel getData() {
    OrderRequestModel model = new OrderRequestModel();
    try {
           model.setImageList(imageDetails);
         } catch (Exception e) {
        SnackUtil.mackText(mBinding.layoutRoot, getString(R.string.some_things_went_wrong), true);
        L.e("date parse Error : " + e.getMessage());
    }
    return model;
}


 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 122:
            if (data != null) {
                if (resultCode == Activity.RESULT_OK) {
                    Bitmap image = (Bitmap) data.getExtras().get("data");
                    String strData = String.valueOf(data.getData());
                    L.e("Camera : " + strData);
                    if (image != null) {
                        ImageDetails imgDetails = new ImageDetails();
                         imgDetails.setName(MyUtil.getFilename(Uri.parse(strData), getActivity()));
                        imgDetails.setBitmap(image);
                        imgDetails.setName("image" + new Random().nextInt(1000));

                        imageDetails.add(imgDetails);
                    }
                }
                adapterImages.notifyDataSetChanged();
            }
            break;
        case 144:
            if (data != null) {
                if (resultCode == Activity.RESULT_OK) {
                    String strData = data.getDataString();
                    Uri[] resultFileChooser = null;
                    try {
                        if (data.getClipData() == null) {
                            L.e("data Clicp is Null");
                        }
                        resultFileChooser = new Uri[data.getClipData().getItemCount()];
                        for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                            ImageDetails details = new ImageDetails();

                            details.setPath(getPath(data.getClipData().getItemAt(i).getUri()));
                            details.setURI(data.getClipData().getItemAt(i).getUri());
                            details.setName(MyUtil.getFilename(data.getClipData().getItemAt(i).getUri(), getActivity()));

                            imageDetails.add(details);

                            L.e("Uri : " + details.getPath());
                        }
                    } catch (NullPointerException e) {
                        if (strData != null) {
                            resultFileChooser = new Uri[]{Uri.parse(strData)};
                            ImageDetails imgDetails = new ImageDetails();
                            imgDetails.setName(MyUtil.getFilename(Uri.parse(strData), getActivity()));
                            imgDetails.setPath(getPath(Uri.parse(strData)));
                            imgDetails.setURI(Uri.parse(strData));
                            imageDetails.add(imgDetails);
                            L.e("Uri : " + imgDetails.getPath());

                        }
                    }
                }
                adapterImages.notifyDataSetChanged();
            }
            break;
    }
}

this is the code which i am trying i paste my onActivity code and multipart code also.

you may want to enable multi-selection while selection and then syncing with the server.

 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setType("image/*");
        startActivityForResult(intent, READ_REQUEST_CODE);

you will receive multiple Uri in your onActivity from there use to get file objects and then sync with the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM