WebView 选择相册图片踩坑
来源:互联网 发布:linux 启动过程 编辑:程序博客网 时间:2024/05/17 04:06
最近在为公司的几个H5项目做app打包,然后就踩到这个坑了。
项目中有 h5调用相册选择图片,然后上传这个功能,测试说上传不了图片。然后搜了不少资料,折腾了一下午,终于搞好了,特此记录下。
坑1:原生WebView 的选择文件功能需要我们自己实现
代码如下:
wv_main.setWebChromeClient(new WebChromeClient() { // Andorid 4.1----4.4 public void openFileChooser(ValueCallback<uri> uploadFile, String acceptType, String capture) { mFilePathCallback = uploadFile; handle(uploadFile); } // for 5.0+ @Override public boolean onShowFileChooser(WebView webView, ValueCallback<uri[]> filePathCallback, FileChooserParams fileChooserParams) { if (mFilePathCallbackArray != null) { mFilePathCallbackArray.onReceiveValue(null); } mFilePathCallbackArray = filePathCallback; handleup(filePathCallback); return true; } }); } private void handle(ValueCallback<uri> uploadFile) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, PICK_REQUEST); } private void handleup(ValueCallback<uri[]> uploadFile) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, PICK_REQUEST); }
然后在Activity的onActivityResult 方法里处理回调,代码如下:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_REQUEST) { if (null != data) { Uri uri = data.getData(); handleCallback(uri); } else { // 取消了照片选取的时候调用 handleCallback(null); } } else { // 取消了照片选取的时候调用 handleCallback(null); } }/** * 处理WebView的回调 * * @param uri */ private void handleCallback(Uri uri) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (mFilePathCallbackArray != null) { if (uri != null) { mFilePathCallbackArray.onReceiveValue(new Uri[]{uri}); } else { mFilePathCallbackArray.onReceiveValue(null); } mFilePathCallbackArray = null; } } else { if (mFilePathCallback != null) { if (uri != null) { String url = getFilePathFromContentUri(uri, getContentResolver()); Uri u = Uri.fromFile(new File(url)); mFilePathCallback.onReceiveValue(u); } else { mFilePathCallback.onReceiveValue(null); } mFilePathCallback = null; } } }
坑2:记得一定要释放,不然点取消之后,就再调不起来了(一下午主要就填这个坑了)
4.1-4.4 mFilePathCallback.onReceiveValue(null);5.0mFilePathCallbackArray.onReceiveValue(null);
坑3:4.4以下,返回的Uri 是content: 开头的,h5端识别不了,需要转成绝对路径
public static String getFilePathFromContentUri(Uri selectedVideoUri, ContentResolver contentResolver) { String filePath; String[] filePathColumn = {MediaStore.MediaColumns.DATA}; Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);// 也可用下面的方法拿到cursor// Cursor cursor = this.context.managedQuery(selectedVideoUri, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); filePath = cursor.getString(columnIndex); cursor.close(); return filePath; }
你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢
0 0
- WebView 选择相册图片踩坑
- Android WebView 选择图片并上传(调用相机拍照/相册/选择文件)
- 图片选择------本地相册
- 从相册选择图片
- webView选择图片遇到的坑
- webView选择本地图片
- jquery相册图片来回选择
- Android 手机相册图片选择
- Android 从相册选择图片
- 从相册中选择图片
- 相机或相册选择图片
- unity 打开相册选择图片
- 长按 WebView 页面图片弹出对话框可以选择保存长按的图片到本地相册
- webview调本地相册上传图片
- android 选择图片 相册选择 马上拍照
- Android 选择图片 相册选择 马上拍照
- 关于Android 选择相册图片裁剪的坑
- android 相册选择图片 图片的压缩
- 指数爆炸 体验幂的可怕
- 1025. PAT Ranking (25)
- Android 各种按钮点击效果以及兼容性问题总结
- python unittest做api自动化测试
- 浏览器缓存机制(一)——HTTP缓存
- WebView 选择相册图片踩坑
- linux设备驱动之异步通知
- python的with open as f
- android自定义签名文件
- boost.log要点笔记
- codeforces-768D-Jon and Orbs(概率DP)
- Java反射机制详解
- 归并排序
- Spring整合Hessian