0

I can choose single image from gallery but can't choose multiple images.
I google information about that and found add EXTRA_ALLOW_MULTIPLE to putExtra, but it doesn't work for me. Can anyone help me?
My problem is file upload on webview. I am targeting at os6, os7 or os8.

my code :

    mWebview.setWebChromeClient(new WebChromeClient() {
        protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            i.setAction(Intent.ACTION_PICK);
            startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
        }

update Thanks everyone. Finally, I can choose both single image or multiple images from gallery. This is my solution, I need to clean up unnecessary code though.

    mWebview.setWebChromeClient(new WebChromeClient() {
        protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.setType("image/*");
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
            if (uploadMessage != null) {
                uploadMessage.onReceiveValue(null);
                uploadMessage = null;
            }

            uploadMessage = filePathCallback;

            Intent intent = fileChooserParams.createIntent();
            intent.setType("image/*");
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            try {
                startActivityForResult(intent, REQUEST_SELECT_FILE);
            } catch (ActivityNotFoundException e) {
                uploadMessage = null;
                Toast.makeText(MainActivity.this.getApplicationContext(), "can't choose files", Toast.LENGTH_LONG).show();
                return false;
            }
            return true;
        }
    });


@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (requestCode == REQUEST_SELECT_FILE) {
            if (uploadMessage == null) return;

            ClipData clipData = intent.getClipData();
            if (clipData != null && clipData.getItemCount() > 0) {
                Uri[] results = new Uri[clipData.getItemCount()];
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    ClipData.Item item = clipData.getItemAt(i);
                    results[i] = item.getUri();
                }

                uploadMessage.onReceiveValue(results);
                uploadMessage = null;
            } else {
                uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
                uploadMessage = null;
            }

        }
    } else if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage) return;

        Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    } else {
        Toast.makeText(MainActivity.this.getApplicationContext(), "fail to upload files", Toast.LENGTH_LONG).show();
    }
}
Ken
  • 109
  • 9

3 Answers3

0

use image picker

 ImagePicker.create(UploadPhotosActivity.this)
            .showCamera(false)
            .limit(2)  // set your limit here
            .imageTitle(getString(R.string.select_image))
            .folderTitle(getString(R.string.folder))
            .theme(R.style.ImagePickerTheme)
            .start(RC_CODE_PICKER);  

add these to your dependency

compile 'com.github.esafirm.android-image-picker:imagepicker:1.5.0'
compile 'com.github.esafirm.android-image-picker:rximagepicker:1.5.0'
Kevin Kurien
  • 812
  • 6
  • 14
0
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image*//*");
String[] extraMimeTypes = {"image/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeTypes);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, REQUEST_CODE_GALLERY);

Try this. Working in all versions.

Jayanth vn
  • 115
  • 2
  • 12
  • Since `ACTION_OPEN_DOCUMENT` was only added in API Level 19, and since `image*//*` is not a valid MIME type, this code definitely does not work "in all versions". – CommonsWare Nov 19 '18 at 12:02
  • @CommonsWare I mean whatever versions he required. He mentioned " I am targeting at os6, os7 or os8". – Jayanth vn Nov 21 '18 at 10:16
  • How do you limit maximum number of images to pick? – shoopi Aug 01 '19 at 01:45
0

Edit your code i.e. Remove:

i.setAction(Intent.ACTION_PICK);

Follow below code :

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"), 1);
Rishav Singla
  • 485
  • 4
  • 10