1 wvWebview.setWebChromeClient(new WebChromeClient() { 2 @Override 3 public void onProgressChanged(WebView view, int newProgress) { 4 if (newProgress == 100) { 5 bar.setVisibility(View.INVISIBLE); 6 // line.setVisibility(View.VISIBLE); 7 } else { 8 if (View.INVISIBLE == bar.getVisibility()) { 9 bar.setVisibility(View.VISIBLE); 10 } 11 bar.setVisibility(View.INVISIBLE); 12 bar.setProgress(newProgress); 13 bar.setVisibility(View.VISIBLE); 14 } 15 super.onProgressChanged(view, newProgress); 16 } 17 18 /** 19 * 8(Android 2.2) <= API <= 10(Android 2.3)回调此方法 20 */ 21 private void openFileChooser(android.webkit.ValueCallback<Uri> uploadMsg) { 22 Log.e("WangJ", "运行方法 openFileChooser-1"); 23 // (2)该方法回调时说明版本API < 21,此时将结果赋值给 mUploadCallbackBelow,使之 != null 24 mUploadCallbackBelow = uploadMsg; 25 takePhoto(); 26 } 27 28 /** 29 * 11(Android 3.0) <= API <= 15(Android 4.0.3)回调此方法 30 */ 31 public void openFileChooser(android.webkit.ValueCallback<Uri> uploadMsg, String acceptType) { 32 Log.e("WangJ", "运行方法 openFileChooser-2 (acceptType: " + acceptType + ")"); 33 // 这里我们就不区分input的参数了,直接用拍照 34 openFileChooser(uploadMsg); 35 } 36 37 /** 38 * 16(Android 4.1.2) <= API <= 20(Android 4.4W.2)回调此方法 39 */ 40 public void openFileChooser(android.webkit.ValueCallback<Uri> uploadMsg, String acceptType, String capture) { 41 Log.e("WangJ", "运行方法 openFileChooser-3 (acceptType: " + acceptType + "; capture: " + capture + ")"); 42 // 这里我们就不区分input的参数了,直接用拍照 43 openFileChooser(uploadMsg); 44 } 45 46 /** 47 * API >= 21(Android 5.0.1)回调此方法 48 */ 49 @Override 50 public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback, FileChooserParams fileChooserParams) { 51 Log.e("WangJ", "运行方法 onShowFileChooser"); 52 // (1)该方法回调时说明版本API >= 21,此时将结果赋值给 mUploadCallbackAboveL,使之 != null 53 mUploadCallbackAboveL = valueCallback; 54 takePhoto(); 55 return true; 56 } 57 58 });
1 /** 2 * 调用相机 3 */ 4 private void takePhoto() { 5 // 指定拍照存储位置的方式调起相机 6 String filePath = Environment.getExternalStorageDirectory() + File.separator 7 + Environment.DIRECTORY_PICTURES + File.separator; 8 String fileName = "IMG_" + DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg"; 9 imageUri = Uri.fromFile(new File(filePath + fileName)); 10 11 // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 12 // intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 13 // startActivityForResult(intent, REQUEST_CODE); 14 15 16 // 选择图片(不包括相机拍照),则不用成功后发刷新图库的广播 17 // Intent i = new Intent(Intent.ACTION_GET_CONTENT); 18 // i.addCategory(Intent.CATEGORY_OPENABLE); 19 // i.setType("image/*"); 20 // startActivityForResult(Intent.createChooser(i, "Image Chooser"), REQUEST_CODE); 21 22 Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 23 captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 24 25 Intent Photo = new Intent(Intent.ACTION_PICK, 26 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 27 28 Intent chooserIntent = Intent.createChooser(Photo, "Image Chooser"); 29 chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent}); 30 31 startActivityForResult(chooserIntent, 99); 32 }
1 /** 2 * Android API < 21(Android 5.0)版本的回调处理 3 * @param resultCode 4 * 选取文件或拍照的返回码 5 * @param data 6 * 选取文件或拍照的返回结果 7 */ 8 private void chooseBelow(int resultCode, Intent data) { 9 Log.e("WangJ", "返回调用方法--chooseBelow"); 10 11 if (RESULT_OK == resultCode) { 12 updatePhotos(); 13 14 if (data != null) { 15 // 这里是针对文件路径处理 16 Uri uri = data.getData(); 17 if (uri != null) { 18 Log.e("WangJ", "系统返回URI:" + uri.toString()); 19 mUploadCallbackBelow.onReceiveValue(uri); 20 } else { 21 mUploadCallbackBelow.onReceiveValue(null); 22 } 23 } else { 24 // 以指定图像存储路径的方式调起相机,成功后返回data为空 25 Log.e("WangJ", "自定义结果:" + imageUri.toString()); 26 mUploadCallbackBelow.onReceiveValue(imageUri); 27 } 28 } else { 29 mUploadCallbackBelow.onReceiveValue(null); 30 } 31 mUploadCallbackBelow = null; 32 } 33 34 /** 35 * Android API >= 21(Android 5.0) 版本的回调处理 36 * @param resultCode 37 * 选取文件或拍照的返回码 38 * @param data 39 * 选取文件或拍照的返回结果 40 */ 41 private void chooseAbove(int resultCode, Intent data) { 42 Log.e("WangJ", "返回调用方法--chooseAbove"); 43 44 if (RESULT_OK == resultCode) { 45 updatePhotos(); 46 47 if (data != null) { 48 // 这里是针对从文件中选图片的处理 49 Uri[] results; 50 Uri uriData = data.getData(); 51 if (uriData != null) { 52 results = new Uri[]{uriData}; 53 for (Uri uri : results) { 54 Log.e("WangJ", "系统返回URI:" + uri.toString()); 55 } 56 mUploadCallbackAboveL.onReceiveValue(results); 57 } else { 58 mUploadCallbackAboveL.onReceiveValue(null); 59 } 60 } else { 61 Log.e("WangJ", "自定义结果:" + imageUri.toString()); 62 mUploadCallbackAboveL.onReceiveValue(new Uri[]{imageUri}); 63 } 64 } else { 65 mUploadCallbackAboveL.onReceiveValue(null); 66 } 67 mUploadCallbackAboveL = null; 68 } 69 70 private android.webkit.ValueCallback<Uri[]> mUploadCallbackAboveL; 71 private android.webkit.ValueCallback<Uri> mUploadCallbackBelow; 72 73 private void updatePhotos() { 74 // 该广播即使多发(即选取照片成功时也发送)也没有关系,只是唤醒系统刷新媒体文件 75 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 76 intent.setData(imageUri); 77 sendBroadcast(intent); 78 }
这样配置 即可 稳稳当当 过