1.申请需要的权限
这块注意权限动态申请。
2.初始化webview
private ValueCallbackuploadFile;//定义接受返回值 private ValueCallback uploadFiles;
WebSettings webSettings = webview.getSettings(); webSettings.setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); } @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onPermissionRequest(PermissionRequest request) { // super.onPermissionRequest(request);//必须要注视掉 request.grant(request.getResources()); } // For Android 3.0+ public void openFileChooser(ValueCallbackuploadMsg, String acceptType) { uploadFile = uploadFile; //打开相册或者拍照 openFileOrTakePhoto(); } // For Android < 3.0 public void openFileChooser(ValueCallback uploadMsgs) { uploadFile = uploadFile; openFileOrTakePhoto(); } // For Android > 4.1.1 public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { uploadFile = uploadFile; openFileOrTakePhoto(); } // For Android >= 5.0 @Override public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { uploadFiles = filePathCallback; openFileOrTakePhoto(); return true; } });
/** * /这块可以用一个弹出控件选择相册还是拍照 注意当取消这次操作的时候一定要将uploadFile与uploadFiles重新为null,不然将无法再次打开相册或者拍照代码为:
if (uploadFile != null) { uploadFile.onReceiveValue(null); uploadFile = null; } if (uploadFiles != null) { uploadFiles.onReceiveValue(null); uploadFiles = null; }
*/ private void openFileOrTakePhoto() { //打开相册的代码 Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "Choose"), CHOOSE_REQUEST_CODE); //调取拍照的代码这块注意7.0之后通过FileProvider获取文件
String cameraSavePath = new File(Environment.getExternalStorageDirectory().getPath() + "/" + System.currentTimeMillis() + ".jpg"); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //第二个参数为 包名.fileprovider imageUri = FileProvider.getUriForFile(WebActivity.this, MyApplication.getContext().getPackageName()+".fileprovider", cameraSavePath); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { imageUri = Uri.fromFile(cameraSavePath); } intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, FILE_CAMERA_RESULT_CODE);
}
/** * 重写onActivityResult获取返回值 */ @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { switch (requestCode) { case CHOOSE_REQUEST_CODE: if (null != uploadFile) { Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData(); uploadFile.onReceiveValue(result); uploadFile = null; } if (null != uploadFiles) { Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData(); uploadFiles.onReceiveValue(new Uri[]{result}); uploadFiles = null; } break; case FILE_CAMERA_RESULT_CODE: if (null == uploadFile && null == uploadFiles) return; Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData(); if (uploadFiles != null) { onActivityResultAboveL(requestCode, resultCode, data); } else if (uploadFile != null) { uploadFile.onReceiveValue(result); uploadFile = null; } break; default: break; } } else if (resultCode == Activity.RESULT_CANCELED) { if (null != uploadFile) { uploadFile.onReceiveValue(null); uploadFile = null; } if (null != uploadFiles) { uploadFiles.onReceiveValue(null); uploadFiles = null; } } super.onActivityResult(requestCode, resultCode, data); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) { if (requestCode != FILE_CAMERA_RESULT_CODE || uploadFiles == null) { return; } Uri[] results = null; if (resultCode == Activity.RESULT_OK) { if (data == null) { results = new Uri[]{imageUri}; } else { String dataString = data.getDataString(); ClipData clipData = data.getClipData(); if (clipData != null) { results = new Uri[clipData.getItemCount()]; for (int i = 0; i < clipData.getItemCount(); i++) { ClipData.Item item = clipData.getItemAt(i); results[i] = item.getUri(); } } if (dataString != null) results = new Uri[]{Uri.parse(dataString)}; } } uploadFiles.onReceiveValue(results); uploadFiles = null; }