Android WebView中的文件选择器在Lollipop和Marshmallow无法打开

4
我正在开发一个应用程序,使用 WebView 访问一些逻辑。这个逻辑有一个部分是用户可以上传图片,但我似乎找不到让应用程序这样做的方法。
我使用的示例代码在此 链接 中。
正如您所看到的,从 WebChromeClient 设置了自定义的 Intent,但是文件选择器根本没有打开。 代码如下:
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){  

                // Update message
                mUploadMessage = uploadMsg;

                try{    

                    // Create AndroidExampleFolder at sdcard

                    File imageStorageDir = new File(
                                           Environment.getExternalStoragePublicDirectory(
                                           Environment.DIRECTORY_PICTURES)
                                           , "AndroidExampleFolder");

                    if (!imageStorageDir.exists()) {
                        // Create AndroidExampleFolder at sdcard
                        imageStorageDir.mkdirs();
                    }

                    // Create camera captured image file path and name 
                    File file = new File(
                                    imageStorageDir + File.separator + "IMG_"
                                    + String.valueOf(System.currentTimeMillis()) 
                                    + ".jpg");

                    mCapturedImageURI = Uri.fromFile(file); 

                    // Camera capture image intent
                    final Intent captureIntent = new Intent(
                                                  android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

                    Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
                    i.addCategory(Intent.CATEGORY_OPENABLE);
                    i.setType("image/*");

                    // Create file chooser intent
                    Intent chooserIntent = Intent.createChooser(i, "Image Chooser");

                    // Set camera intent to file chooser 
                    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
                                           , new Parcelable[] { captureIntent });

                    // On select image call onActivityResult method of activity
                    startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);

                  }
                 catch(Exception e){
                     Toast.makeText(getBaseContext(), "Exception:"+e, 
                                Toast.LENGTH_LONG).show();
                 }

            }

提前感谢!
编辑

所以,经过@JoxTraex的启示,我意识到文件选择器没有触发,因为我没有覆盖WebChromeClient的任何方法。现在,文件选择器正在提示,但是,如果我选择一个图像,则该图像不会被上传。
我用于文件选择器的代码:

@Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                openFileChooser(filePathCallback, "");
                return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
            }

            public void openFileChooser(ValueCallback<Uri[]> uploadMsg, String acceptType){
                // Update message
                //mUploadMessage = uploadMsg.this;
                try{
                    // Create AndroidExampleFolder at sdcard
                    File imageStorageDir = new File(
                            Environment.getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_PICTURES)
                            , "AndroidExampleFolder");

                    if (!imageStorageDir.exists()) {
                        // Create AndroidExampleFolder at sdcard
                        imageStorageDir.mkdirs();
                    }

                    // Create camera captured image file path and name
                    File file = new File(
                            imageStorageDir + File.separator + "IMG_"
                                    + String.valueOf(System.currentTimeMillis())
                                    + ".jpg");

                    mCapturedImageURI = Uri.fromFile(file);

                    // Camera capture image intent
                    final Intent captureIntent = new Intent(
                            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

                    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                    i.addCategory(Intent.CATEGORY_OPENABLE);
                    i.setType("image/*");

                    // Create file chooser intent
                    Intent chooserIntent = Intent.createChooser(i, "Image Chooser");

                    // Set camera intent to file chooser
                    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
                            , new Parcelable[] { captureIntent });

                    // On select image call onActivityResult method of activity
                    startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);

                }
                catch(Exception e){
                    Toast.makeText(getBaseContext(), "Exception:"+e,
                            Toast.LENGTH_LONG).show();
                }

            }

提前致谢!


1
你确定有些应用程序能够处理那个特定的意图吗? - JoxTraex
@JoxTraex 我在清单文件中添加了一些权限,例如互联网权限、读取外部存储和相机权限。 - pamobo0609
@JoxTraex,我刚刚按照你的建议运行了应用程序,并添加了日志,但是它没有记录任何内容... - pamobo0609
1
我会从这里开始 @ https://developer.android.com/studio/debug/index.html。 - JoxTraex
1
我不确定这是否可能(可能是错误的),但当你启动意图时,如果有人能够处理它,他们会处理。两种可能的解释是:1)你期望的代码路径没有显示;2)你构建的意图无法被任何人服务。在没有任何安全假设的情况下,我们只能猜测你的问题所在。如果你希望得到帮助,你需要提供更明确的指导,特别是如果你知道可能出现的问题,将其包含在你的帖子中将有助于某人验证你的假设。 - JoxTraex
显示剩余6条评论
2个回答

9

好的,在一些监听器的帮助下,我找到了一个解决我的问题的代码。不过还是要感谢@JoxTraex和这个人


这在 Nexus 上无法工作,有什么解决办法吗?只有从相册中选择的选项可以工作,但是从相机中打开后,照片没有被选中或者什么也没有发生。如果您已经解决了这个问题,或者有任何想法,请分享一下。 - Satan Pandeya
@SatanPandeya,我最近在我的Nexus 5X上测试了这段代码,一切似乎都很好。不过,我将创建一个gist,其中包含我为此编写的最新代码。我会回来的。 - pamobo0609
@SatanPandeya 这是要点:https://gist.github.com/pamobo0609/1650a73d0478f5aeabc95f67f1457ba5 如果您有任何疑问,请告诉我。 - pamobo0609

5
被接受的答案是正确的,但如果有帮助的话,我将发布我的代码。我使用了下面的代码,需要添加到webChrome类中。
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
            mFilePathCallback = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
            return true;
        }

并在onActivityResult中使用该代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (requestCode != FILECHOOSER_RESULTCODE || mFilePathCallback == null) {
            super.onActivityResult(requestCode, resultCode, intent);
            return;
        }
        Uri[] results = null;
        // Check that the response is a good one
        if (resultCode == Activity.RESULT_OK) {
                String dataString = intent.getDataString();
                if (dataString != null) {
                    results = new Uri[]{Uri.parse(dataString)};
            }
        }
        mFilePathCallback.onReceiveValue(results);
        mFilePathCallback = null;
    }

您还需要适用于Marshmallow及以上版本的运行时权限。 在Lollipop中,此代码将正常工作。 您还需要添加其他Android版本的条件以使其在其他版本上运行。


非常适用于API29!! - Popularfan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接