如何从Android的图库中上传多个图像?

时间:2018-07-26 07:47:17

标签: android android-volley retrofit2

我想从图库向服务器上传多张图片,但只获得一张而不是多张图片。

以下是代码

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);
}

我尝试了很多,但是只上传了一张图片,请帮助我摆脱过去三天以来的困扰。

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;
    }
}

这是我尝试粘贴的onActivity代码和多部分代码的代码。

1 个答案:

答案 0 :(得分:0)

您可能希望在选择时启用多选,然后与服务器同步。

 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);

您将在onActivity中收到多个Uri,用于获取文件对象,然后与服务器同步。