解决WebView不支持input type=file
2017-05-21 15:25
1346 查看
1.定义activity
2.重写shouldOverrideUrlLoading
3. 使用返回键的方式防止网页重定向
4.重写WebChromeClient
5.回调
private ValueCallback<Uri> mUploadMessage; public ValueCallback<Uri[]> uploadMessage; public static final int REQUEST_SELECT_FILE = 100; private final static int FILECHOOSER_RESULTCODE = 2;
2.重写shouldOverrideUrlLoading
webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return false; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { } @Override public void onPageFinished(WebView view, String url) { } });
3. 使用返回键的方式防止网页重定向
webView.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) { webView.goBack(); return true; } } return false; } });
4.重写WebChromeClient
webView.setWebChromeClient(new WebChromeClient() { // For 3.0+ Devices (Start) // onActivityResult attached before constructor protected void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); } // For Lollipop 5.0+ Devices @TargetApi(Build.VERSION_CODES.LOLLIPOP) public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, 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(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); return false; } return true; } //For Android 4.1 only protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE); } protected void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } });
5.回调
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_SELECT_FILE: if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (uploadMessage == null) return; uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); uploadMessage = null; } break; case FILECHOOSER_RESULTCODE: if (null == mUploadMessage) return; // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment // Use RESULT_OK only if you're implementing WebView inside an Activity Uri result = data == null || resultCode != MainActivity.RESULT_OK ? null : data.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; break; } }
相关文章推荐
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- 解决webview不支持input type=file
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView不支持H5输入type =“file”解决方法
- Android:让WebView支持<input type=”file”…>元素
- 让 Android 的 WebView 支持 type 为 file 的 input,同时支持拍照
- Android:让WebView支持<input type=”file”…>元素 - GreatK
- [Cordova/Phonegap] 改进InAppBrowser插件(WebView),让其<input type="file">支持选择文件
- Android WebView H5页面 input type =“file”解决方法
- android Webview支持input type=file
- android webview支持<input type="file"类型
- Android WebView 支持H5图片上传<input type="file">
- WebView支持<input type="file">调起本地文件,获取图片
- android Webview支持input type=file
- Android:让WebView支持<input type=”file”…>元素
- 【Android进阶】Android里webview不支持input file的解决方法(上传按钮点击失效的原因)
- Android-WebView-解决对选择文件<input type="file">无响应
- WebView中 支持input type=file