来自画廊的多个图像pic

时间:2018-05-17 10:47:08

标签: android image gallery

我正在开发一个项目,我必须从库中选择多张照片,并希望将它们保存在imageview数组中。我无法从imageview中选择任何图像。任何人都可以告诉我,我如何导入多个图像并将其保存在数组中?

     private void chooseimage() {
            final CharSequence[] items = {"Take Photo", "Choose from Library",
                    "Cancel"};

            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ActionStatusActivity.this);
            builder.setTitle("Add Photo!");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    if (items[item].equals("Take Photo")) {
                        userChoosenTask = "Take Photo";
                        dialog.dismiss();
                        cameraIntent();
                    } else if (items[item].equals("Choose from Library")) {
                        userChoosenTask = "Choose from Library";
                        dialog.dismiss();
                      picimage();
                        //galleryIntent();

                    } else if (items[item].equals("Cancel")) {
                        dialog.dismiss();
                    }
                }
            });
            builder.show();
        }
          private void picimage() {
          Intent intent = new Intent();
          intent.setType("image/*");
          intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
          intent.setAction(Intent.ACTION_GET_CONTENT);
          startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);

      }
        **@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == SELECT_FILE && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data
                if(data.getClipData() != null) {
                    int count = data.getClipData().getItemCount(); //evaluate the count before the for loop --- otherwise, the count is evaluated every loop.
                    for(int i = 0; i < count; i++)
                        imageUri = data.getClipData().getItemAt(i).getUri();
                    onSelectFromGalleryResult((List<String>) imageUri);
                    //do something with the image (save it to some directory or whatever you need to do with it here)
                }
            } else if(data.getData() != null) {
                String imagePath = data.getData().getPath();
                //do something with the image (save it to some directory or whatever you need to do with it here)
            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        }**

private void onSelectFromGalleryResult(List<String> data) {
        Bitmap bm = null;
        int position = 0;
        int width = 1;
        int height = 1;
        editArray = new EditText[data.size()];
        mLayoutParams = new RelativeLayout.LayoutParams(mLayoutImage.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
        imageArray = new ImageView[data.size()];
        mLayoutImage.setLayoutParams(mLayoutParams);

        for (int i = 0; i <data.size(); i++) {

            LinearLayout mainLayout=new LinearLayout(getApplicationContext());
            mainLayout.setOrientation(LinearLayout.VERTICAL);


            RelativeLayout.LayoutParams maiParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            maiParams.setMarginStart(200*i);
            editArray[i] = new EditText(ActionStatusActivity.this);
            editArray[i].setTextSize(16);

            imageArray[i] = new ImageView(ActionStatusActivity.this);
            imageArray[i].setId(i);
            editArray[i].setId(i);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    200,
                    200);



            RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
                    200,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);




            imageArray[i].setLayoutParams(params);
            editArray[i].setLayoutParams(textParams);


            new UploadFile().execute(Uri.fromFile(new File(data.get(i))).toString());
            String path = Uri.fromFile(new File(data.get(i))).toString();

            Glide.with(getApplicationContext()).load(Uri.fromFile(new File(data.get(i)))).asBitmap().override(250, 250).centerCrop().into(imageArray[i]);


            mLayoutParams.addRule(RelativeLayout.BELOW, editArray[i].getId());
            mainLayout.addView(imageArray[i]);
            mainLayout.addView( editArray[i]);
            mainLayout.setLayoutParams(maiParams);
            mLayoutImage.addView(mainLayout);
        }
    }

1 个答案:

答案 0 :(得分:0)

Intent intent = new Intent();
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                if (Build.VERSION.SDK_INT < 19) {
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent = Intent.createChooser(intent, "Select file");
                } else {
                    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);


//                    String[] mimetypes = { "*/*"};
                    String[] mimeTypes =
                            {"image/*"};

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
                        if (mimeTypes.length > 0) {
                            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
                        }
                    } else {
                        String mimeTypesStr = "";
                        for (String mimeType : mimeTypes) {
                            mimeTypesStr += mimeType + "|";
                        }
                        intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
                    }

//                    intent.setType("image/*|application/pdf|audio/*");
//                    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
                }
                startActivityForResult(intent,0);`enter code here`