public static final int FILECHOOSER_RESULTCODE = 1;
private static final int REQ_CAMERA = FILECHOOSER_RESULTCODE+1; private static final int REQ_CHOOSE = REQ_CAMERA+1; ValueCallback<Uri[]> mFilePathCallback; private class MyWebChromeClient extends WebChromeClient {
public boolean onShowFileChooser( WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { if (mFilePathCallback != null) return true; mFilePathCallback = filePathCallback; selectImage(); return true; } // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { if (mUploadMessage != null) return; mUploadMessage = uploadMsg; selectImage(); // Intent i = new Intent(Intent.ACTION_GET_CONTENT); // i.addCategory(Intent.CATEGORY_OPENABLE); // i.setType("*/*"); // startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE ); } // 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); }
}
/** * 检查SD卡是否存在 * * @return */ public final boolean checkSDcard() { boolean flag = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); if (!flag) { Toast.makeText(this, "请插入手机存储卡再使用本功能",Toast.LENGTH_SHORT).show(); } return flag; } String compressPath = "";
protected final void selectImage() { if (!checkSDcard()) return; String[] selectPicTypeStr = { "相机","图库" }; AlertDialog alertDialog = new AlertDialog.Builder(this) .setItems(selectPicTypeStr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { // 相机拍摄 case 0: openCarcme(); break; // 手机相册 case 1: chosePic(); break; default: break; } compressPath = Environment .getExternalStorageDirectory() .getPath() + "/fuiou_wmp/temp"; new File(compressPath).mkdirs(); compressPath = compressPath + File.separator + "compress.jpg"; } }).setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialogInterface) { if(mFilePathCallback != null){ Uri[] uris = new Uri[1]; uris[0] = Uri.parse(""); mFilePathCallback.onReceiveValue(uris); mFilePathCallback=null; }else { mUploadMessage.onReceiveValue(Uri.parse("")); mUploadMessage=null; } } }).show(); }
String imagePaths; Uri cameraUri; /** * 打开照相机 */ private void openCarcme() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);