private ValueCallback<Uri> mUploadMessage; private ValueCallback<Uri[]> mUploadMessage5; public static final int FILECHOOSER_RESULTCODE = 5173; public static final int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 5174; ////// private static final int REQUEST_CAMERA = 1; private static final int REQUEST_CHOOSE = 2; //ValueCallback<Uri> mUploadMessage; ValueCallback<Uri[]> mUploadMessagesAboveL; //private WebView mWebView; private Uri cameraUri;
private class MyWebViewClient extends WebViewClient { public MyWebViewClient() { super(); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { Log.d(TAG, "URL地址:" + url); super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { Log.i(TAG, "onPageFinished"); super.onPageFinished(view, url); } } private class MyWebChromeClient extends WebChromeClient { // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { Log.d(TAG,"openFileChooser"); if (mUploadMessage != null) return; mUploadMessage = uploadMsg; selectImage(); } // For Android < 3.0 public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooser(uploadMsg, ""); } // For Android > 4.1.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooser(uploadMsg, acceptType); } // For Android 5.0 @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { if (mUploadMessagesAboveL != null) { mUploadMessagesAboveL.onReceiveValue(null); } else { mUploadMessagesAboveL = filePathCallback; selectImage(); } return true; } } private void selectImage() { if (!FileUtils.checkSDcard(this)) { return; } String[] selectPicTypeStr = {"拍照", "图库"}; new AlertDialog.Builder(this) .setOnCancelListener(new ReOnCancelListener()) .setItems(selectPicTypeStr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { // 相机拍摄 case 0: openCarcme(); break; // 手机相册 case 1: chosePicture(); break; default: break; } } }).show(); } /** * 本地相册选择图片 */ private void chosePicture() { Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); innerIntent.setType("image/*"); Intent wrapperIntent = Intent.createChooser(innerIntent, null); startActivityForResult(wrapperIntent, REQUEST_CHOOSE); } /** * 选择照片后结束 * * @param data */ private Uri afterChosePic(Intent data) { if (data != null) { final String path = data.getData().getPath(); if (path != null && (path.endsWith(".png") || path.endsWith(".PNG") || path.endsWith(".jpg") || path.endsWith(".JPG"))) { //return data.getData(); Log.d(TAG, "afterChosePic: imgData : "+data.getData()); } else { Toast.makeText(this, "上传的图片仅支持png或jpg格式", Toast.LENGTH_SHORT).show(); } } return null; } /** * 打开照相机 */ private void openCarcme() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String imagePaths = Environment.getExternalStorageDirectory().getPath() + "/BigMoney/Images/" + (System.currentTimeMillis() + ".jpg"); // 必须确保文件夹路径存在,否则拍照后无法完成回调 File vFile = new File(imagePaths); if (!vFile.exists()) { File vDirPath = vFile.getParentFile(); vDirPath.mkdirs(); } else { if (vFile.exists()) { vFile.delete(); } } cameraUri = Uri.fromFile(vFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri); startActivityForResult(intent, REQUEST_CAMERA); } /** * 返回文件选择 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (mUploadMessagesAboveL != null) { onActivityResultAboveL(requestCode, resultCode, intent); } if (mUploadMessage == null) return; Uri uri = null; if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) { uri = cameraUri; } if (requestCode == REQUEST_CHOOSE && resultCode == RESULT_OK) { uri = afterChosePic(intent); } mUploadMessage.onReceiveValue(uri); mUploadMessage = null; super.onActivityResult(requestCode, resultCode, intent); } /** * 5.0以后机型 返回文件选择 * * @param requestCode * @param resultCode * @param data */ private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) { Uri[] results = null; if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) { results = new Uri[]{cameraUri}; } if (requestCode == REQUEST_CHOOSE && resultCode == RESULT_OK) { if (data != null) { String dataString = data.getDataString(); if (dataString != null) results = new Uri[]{Uri.parse(dataString)}; } } mUploadMessagesAboveL.onReceiveValue(results); mUploadMessagesAboveL = null; return; } /** * dialog监听类 */ private class ReOnCancelListener implements DialogInterface.OnCancelListener { @Override public void onCancel(DialogInterface dialogInterface) { if (mUploadMessage != null) { mUploadMessage.onReceiveValue(null); mUploadMessage = null; } if (mUploadMessagesAboveL != null) { mUploadMessagesAboveL.onReceiveValue(null); mUploadMessagesAboveL = null; } } }