关于Android WebView调用onShowFileChooser 无法重复调用的问题

在WebVIew开发中,经常会遇到调⽤⼿机系统相册的场景,不过由于Android版本多的原因,很多系统无法正常打开手机的系统相册。通常,在Web页⾯上点击选择⽂件的控件时,前端一般会使用<input type="file">的写法,此时系统会自动调用WebChromeClient
的openFileChooser()方法(5.0及以上系统会调用onShowFileChooser())来打开打开系统相册。参考代码如下:

 @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
                                         FileChooserParams fileChooserParams) {
            valueCallback = filePathCallback;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(
                    Intent.createChooser(i, "图库"),
                    REQUEST_FILE_SELECT_CODE);
            return true;
        }

然后,在onActivityResult()中将选择的图⽚内容通过ValueCallback的onReceiveVal

猜你喜欢

转载自blog.csdn.net/xiangzhihong8/article/details/125498425