猿问

无法让 android webview 在 android 9 上打开文件选择器

我正在尝试type="file"在移动设备上的应用程序中打开输入文件选择器WebView,但处理同一问题的其他问题给出的解决方案适用于早期的 Android 版本,并且似乎不适用于 Android 9。问题似乎出在这段特定的代码就在这里:


//For Lollipop 5.0+ Devices:  

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)

    {

        if (uploadMessage != null) {

            uploadMessage.onReceiveValue(null);

            uploadMessage = null;

        }


        uploadMessage = filePathCallback;


        Intent intent = fileChooserParams.createIntent();

        try

        {

            startActivityForResult(intent, REQUEST_SELECT_FILE);

        } catch (ActivityNotFoundException e)

        {

            uploadMessage = null;

            Toast.makeText(MainActivity.this, "Error: Unable to open file browser", Toast.LENGTH_LONG).show();

            return false;

        }

        return true;

    }


幕布斯7119047
浏览 148回答 1
1回答

收到一只叮咚

在这里使用此代码修复。&nbsp; &nbsp; &nbsp; &nbsp; // For Lollipop 5.0+ Devices&nbsp; &nbsp; &nbsp; &nbsp; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)&nbsp; &nbsp; &nbsp; &nbsp; public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (uploadMessage != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uploadMessage.onReceiveValue(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uploadMessage = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uploadMessage = filePathCallback;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = fileChooserParams.createIntent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.setAction(Intent.ACTION_GET_CONTENT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.setType("image/*,video/*,audio/*,file/*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(intent, REQUEST_SELECT_FILE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (ActivityNotFoundException e)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uploadMessage = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this, "Error: Unable to open file browser", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答