• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    关于webview适配H5上传照片或者视频文件的方法

    发布者: 网神之王 | 发布时间: 2025-6-16 12:31| 查看数: 96| 评论数: 0|帖子模式

    一、需要实现的功能:
    用H5实现的App中需要在H5获取手机中的照片或者视频文件上传到服务器。

    二、分析实现方法:
    由于不懂前端开发,不知道H5中有 input file之类的标签控件,可以用来选择文件,刚开始的思路还是想着native 端是否要通过提供inputstream流方式,将文件内容传递给JS。后来和前端沟通之后,H5在电脑端都是用input 设置type为 file 来实现文件选择功能,于是才开始搜索资料,发现时需要在webview中设置  setWebChromeClient ,其中有对input 的响应回调:
    三、具体实现:
    前端代码
    1. <input type="file" accept="*/*" name="choose file">
    2. <input type="file" accept="image/*" name="choose image">
    3. <input type="file" accept="video/*" name="choose video">
    4. <input type="file" accept="image/example" name="take photo and upload image">
    5. <input type="file" accept="video/example" name="take video and upload video">
    复制代码
    native端代码:
    1. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    2. @Override
    3. public boolean onShowFileChooser(WebView webView,
    4.                                  ValueCallback<Uri[]> filePathCallback,
    5.                                  WebChromeClient.FileChooserParams fileChooserParams) {
    6.     mFilePathCallbacks = filePathCallback;
    7.     // TODO: 根据标签中得接收类型,启动对应的文件类型选择器
    8.     String[] acceptTypes = fileChooserParams.getAcceptTypes();
    9.     for (String type : acceptTypes) {
    10.         Log.d(TAG, "acceptTypes=" + type);
    11.     }
    12.     // 针对拍照后马上进入上传状态处理
    13.     if ((acceptTypes.length > 0) && acceptTypes[0].equals("image/example")) {
    14.         Log.d(TAG, "onShowFileChooser takePhoto");
    15.         Intent it = CameraFunction.takePhoto(mContext);
    16.         startActivityForResult(it, TAKE_PHOTO_AND_UPLOAD_REQUEST);
    17.         return true;
    18.     }

    19.     // 针对录像后马上进入上传状态处理
    20.     if ((acceptTypes.length > 0) && acceptTypes[0].equals("video/example")) {
    21.         Log.d(TAG, "onShowFileChooser record video");
    22.         Intent it = CameraFunction.recordVideo(mContext);
    23.         startActivityForResult(it, RECORD_VIDEO_AND_UPLOAD_REQUEST);
    24.         return true;
    25.     }

    26.     Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    27.     intent.addCategory(Intent.CATEGORY_OPENABLE);
    28.     if (acceptTypes.length > 0) {
    29.         if (acceptTypes[0].contains("image")) {
    30.             intent.setType("image/*");
    31.         } else if (acceptTypes[0].contains("video")) {
    32.             intent.setType("video/*");
    33.         } else {
    34.             intent.setType("*/*");
    35.         }
    36.     } else {
    37.         intent.setType("*/*");
    38.     }

    39.     WebViewActivity.this.startActivityForResult(Intent.createChooser(intent, "File Chooser"),
    40.             REQUEST_FILE_PICKER);
    41.     return true;
    42. }
    复制代码
    回调设置uri
    1. /**
    2. * 设置input 标签出发的回调选择文件路径,优先使用path参数,
    3. * 其次使用uri参数
    4. * @param uriParam
    5. * @param pathParam
    6. */
    7. private void setFilePathCallback(Uri uriParam, String pathParam) {
    8.     //都为空,则设置null
    9.     if (uriParam == null && pathParam == null) {
    10.         if (mFilePathCallback != null) {
    11.             mFilePathCallback.onReceiveValue(null);
    12.         }
    13.         if (mFilePathCallbacks != null) {
    14.             mFilePathCallbacks.onReceiveValue(null);
    15.         }
    16.     } else if (null != pathParam) { // 优先使用path
    17.         if (mFilePathCallback != null) {
    18.             Uri uri = Uri.fromFile(new File(pathParam));
    19.             mFilePathCallback.onReceiveValue(uri);
    20.         }
    21.         if (mFilePathCallbacks != null) {
    22.             Uri uri = Uri.fromFile(new File(pathParam));
    23.             mFilePathCallbacks.onReceiveValue(new Uri[] { uri });
    24.         }
    25.     } else if (null != uriParam) { //其次使用uri
    26.         if (mFilePathCallback != null) {
    27.             String path = UriUtils.getPath(getApplicationContext(), uriParam);
    28.             Uri uri = Uri.fromFile(new File(path));
    29.             mFilePathCallback.onReceiveValue(uri);
    30.         }
    31.         if (mFilePathCallbacks != null) {
    32.             String path = UriUtils.getPath(getApplicationContext(), uriParam);
    33.             Uri uri = Uri.fromFile(new File(path));
    34.             mFilePathCallbacks.onReceiveValue(new Uri[] { uri });
    35.         }
    36.     }

    37.     mFilePathCallback = null;
    38.     mFilePathCallbacks = null;

    39. }
    复制代码
    针对各个请求场景进行处理:
    1. public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    复制代码
    总结:既然用H5开发APP,就需要了解前端,不懂就要问了。查询方向要对,否则南辕北辙,方向有时候比努力重要!
    到此这篇关于关于webview适配H5上传照片或者视频文件的方法的文章就介绍到这了,更多相关webview适配H5上传照片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/html5/750825.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表