Android WebView中打开相机拍照和选择相册
来源:互联网 发布:淘宝的一元秒杀在哪里 编辑:程序博客网 时间:2024/05/29 10:01
一般在项目中与js交互,可能会遇到上传文件图片等操作,避免不了一些坑,下面简单说一下,Android 在不同版本中webView调用相机,选择相册的方法是不一样的,3.0以下的调用
public void openFileChooser(ValueCallback<Uri> uploadMsg)3.0以上:
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
4.4以下:
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
5.0以上:
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams)
那下面具体的贴下代码:
继承自WebChromeClient,重写
//3.0++public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { openFileChooserImpl(uploadMsg);}//3.0--public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooserImpl(uploadMsg);}//4.1public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooserImpl(uploadMsg);}@Overridepublic boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { openFileChooserImplForAndroid5(filePathCallback); return true;}
外层定义接收返回值:
private ValueCallback<Uri> mUploadMessage;private ValueCallback<Uri[]> mUploadCallbackAboveL;
不同的版本调不同的方法:
private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) { mUploadCallbackAboveL = uploadMsg; dispatchTakePictureIntent();}//5.0以下的掉用private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; dispatchTakePictureIntent();}//拍照private void dispatchTakePictureIntent() { selectImgDialog();}
我下面就把代码全部贴出来吧:
private void takePhoto() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { Uri imageUri = null; try { imageUri = Uri.fromFile(createImageFile()); } catch (IOException e) { e.printStackTrace(); } takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(takePictureIntent, FILECHOOSER_RESULTCODE); } } /** * 209. * 本地相册选择图片 * 210. */ private void chosePic() { Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); String IMAGE_UNSPECIFIED = "image/*"; innerIntent.setType(IMAGE_UNSPECIFIED); // 查看类型 Intent wrapperIntent = Intent.createChooser(innerIntent, null); startActivityForResult(wrapperIntent, REQ_CHOOSE); } String mCurrentPhotoPath = null; String FileName = "forum"; //创建文件夹包装图片 private File createImageFile() throws IOException { File storageDir = new File(Util.getAppPath(getActivity()) + FileName); if (!storageDir.exists()) { storageDir.mkdirs(); } storageDir = new File(Util.getAppPath(getActivity()) + FileName + "/", System.currentTimeMillis() + ".jpg"); //保存当前图片路径 mCurrentPhotoPath = storageDir.getAbsolutePath(); return storageDir; } //onActivityResult回调 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE || requestCode == REQ_CHOOSE) { if (null == mUploadMessage && null == mUploadCallbackAboveL) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (mUploadCallbackAboveL != null) { onActivityResultAboveL(requestCode, data); } else if (mUploadMessage != null) { mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } } //5.0以上版本,由于api不一样,要单独处理// @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void onActivityResultAboveL(int requestCode, Intent data) { if (mUploadCallbackAboveL == null) { return; } Uri result = null; if (requestCode == FILECHOOSER_RESULTCODE) { File file = new File(mCurrentPhotoPath); Uri localUri = Uri.fromFile(file); Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri); getActivity().sendBroadcast(localIntent); result = Uri.fromFile(file); } else if (requestCode == REQ_CHOOSE) { result = data.getData(); } mUploadCallbackAboveL.onReceiveValue(new Uri[]{result}); mUploadCallbackAboveL = null; return; }
基本代码就是以上这些,selectImgDialog()方法是弹窗提示选择,关于这个弹窗布局的用法,之前写的一篇文章说的很清楚了(BottomSheet 的详解及注意事项),有疑问的欢迎提问。
1 0
- Android WebView中打开相机拍照和选择相册
- Android WebView 选择图片并上传(调用相机拍照/相册/选择文件)
- 在Android中通过WebView调用相机拍照/选择文件
- 调用系统相机拍照和选择相册
- android调用系统相机拍照与打开相册选择照片进行截取保存笔记
- Android 拍照和相册选择
- android 打开相机和相册
- Android调用系统相册和相机拍照
- android 拍照相册选择 以及android6.0打开相册失败
- 关于android webview调用相册和相机
- 关于android webview调用相册和相机
- 在Android浏览器中通过WebView调用相机拍照/选择文件 上传到服务器
- 在Android浏览器中通过WebView调用相机拍照/选择文件 上传到服务器
- Android webview支持type="file" 打开相机和相册,支持小米(亲测6.0通过)
- (转载)Android webview支持type="file" 打开相机和相册
- Android webview支持type="file" 打开相机和相册兼容5.0+
- android开发 webview支持type="file" 打开相机和相册
- Android 中拍照、相册选择、裁剪照片
- iOS NSDate与NSString相互转化
- IOS-OC App本地数据库
- Java内存泄漏与内存溢出
- JVM学习系列(一)——基础知识
- 年度最牛广告文案,看到最后我服了
- Android WebView中打开相机拍照和选择相册
- 骑士旅行问题-java解答
- HDU 1548 <BFS+标记+找最短>
- 《将博客搬至CSDN》
- maven仓库
- 利用润乾时间日期函数实现日期扩展功能
- MySQL常用命令总结
- Java中的final
- 打包文件