伪代码贴上
@Override protected void onCreate(Bundle savedInstanceState) { mWebView = (WebView) findViewById(R.id.webview); //获得webView initWebView(); }
private void initWebView() { WebSettings settings = mWebView.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(false); settings.setBuiltInZoomControls(false); mWebView.setWebChromeClient(new WebChromeClient() { // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { Log.i("test", "openFileChooser 1"); SplashActivity.this.uploadFile = uploadMsg; openFileChooseProcess(); } // For Android < 3.0 public void openFileChooser(ValueCallback<Uri> uploadMsgs) { Log.i("test", "openFileChooser 2"); SplashActivity.this.uploadFile = uploadMsgs; openFileChooseProcess(); } // For Android > 4.1.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { Log.i("test", "openFileChooser 3"); SplashActivity.this.uploadFile = uploadMsg; openFileChooseProcess(); } // For Android >= 5.0 public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { Log.i("test", "openFileChooser 4:" + filePathCallback.toString()); SplashActivity.this.uploadFiles = filePathCallback; openFileChooseProcess(); return true; } }); } private void openFileChooseProcess() { // 这里可以做你想做的事情了,比如打开相机 选择相册,或者调用第三方的插件 }
这里返回2个值就可以把图片路径传回去了,也可以操作的时候把Uri定义在外面,具体看选择的方法
private ValueCallback<Uri> uploadFile; private ValueCallback<Uri[]> uploadFiles; @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data);if (requestCode == 664) { if (resultCode == RESULT_OK) { if (null != uploadFile) { Uri result = data == null ? null : data.getData(); uploadFile.onReceiveValue(result); uploadFile = null; } if (null != uploadFiles) { Uri result = data == null ? null : data.getData(); uploadFiles.onReceiveValue(new Uri[]{result}); uploadFiles = null; } } else if (resultCode == RESULT_CANCELED) { if (null != uploadFile) { uploadFile.onReceiveValue(null); uploadFile = null; } } } }