如何从图库上传多张图片?

时间:2018-11-19 11:03:04

标签: android android-intent

我可以从图库中选择一张图片,但不能选择多张图片。
我在Google上搜索了有关该信息,并发现将EXTRA_ALLOW_MULTIPLE添加到putExtra ,但这对我不起作用。谁能帮我吗?
我的问题是在webview上上传文件。我的目标是os6,os7或os8。

我的代码:

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

更新 感谢大家。最后,我可以从图库中选择单个图像或多个图像。这是我的解决方案,但是我需要清理不必要的代码。

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

3 个答案:

答案 0 :(得分:0)

使用图像选择器

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

将这些添加到您的依赖项

compile 'com.github.esafirm.android-image-picker:imagepicker:1.5.0'
compile 'com.github.esafirm.android-image-picker:rximagepicker:1.5.0'

答案 1 :(得分: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);

尝试一下。适用于所有版本。

答案 2 :(得分:0)

编辑代码,即删除:

i.setAction(Intent.ACTION_PICK);

遵循以下代码:

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