android webview js交互,调取本地相册,并且实现上传。
首先我们要理解整个互动过程,在网页webview的的组件是 input输入框,输入类型为file,点击-----> 调用安卓不同的版本
onShowFileChooser(5.0专用的方法,是隐性的),openFileChooser(3.0,4.0)这些都要写进去,是为了针对不同的安卓版本。
----->启动android系统自带的相册选择 ---->选择完成之后,回调 onActivityResult,这个方法很重要,里面有针对不同安卓版本的处理,特别是5.0 ,返回来的是一个URI数组,需要进行处理。 废话不说上代码:
webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder b2 = new AlertDialog.Builder(WebViewActivity.this) .setTitle("友情提示:").setMessage(message) .setPositiveButton("ok", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); // MyWebView.this.finish(); } }); b2.setCancelable(false); b2.create(); b2.show(); return true; } @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { umUploadMessages = filePathCallback; mfileChooserParams = fileChooserParams; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.putExtra("return-data", true); i.setType("image/*"); i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(Intent.createChooser(i, "选择相册"), FILECHOOSER_RESULTCODE_5); return true; } // For Android 3.0 public void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.putExtra("return-data", true); i.setType("image/*"); i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(Intent.createChooser(i, "选择相册"), FILECHOOSER_RESULTCODE); } // For Android > 4.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { Toast.makeText(WebViewActivity.this,"你大爷的",Toast.LENGTH_SHORT).show(); mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.putExtra("return-data", true); i.setType("image/*"); i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(Intent.createChooser(i, "选择相册"), FILECHOOSER_RESULTCODE); } // Andorid 3.0 + public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.putExtra("return-data", true); i.setType("image/*"); i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(Intent.createChooser(i, "选择相册"), FILECHOOSER_RESULTCODE); } });
回调处理:
/** * 取照片完成之后的回选调 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; if (null == data){ mUploadMessage.onReceiveValue(null); mUploadMessage = null; }else{ Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } }else if (requestCode == FILECHOOSER_RESULTCODE_5){// 5.0适配,主要是因为5.0的返回参数不同。 // 处理5.0的callback if (umUploadMessages != null) { if (null != data) {// 5.0的处理方式不同,要注意。 ArrayList<String> resultList = data .getStringArrayListExtra("data"); umUploadMessages.onReceiveValue( mfileChooserParams.parseResult(resultCode, data)); umUploadMessages = null; } else { umUploadMessages.onReceiveValue(null); } } } }