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


Posted in HTML / CSS onNovember 04, 2020

一、需要实现的功能:

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

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

二、分析实现方法:

由于不懂前端开发,不知道H5中有 input file之类的标签控件,可以用来选择文件,刚开始的思路还是想着native 端是否要通过提供inputstream流方式,将文件内容传递给JS。后来和前端沟通之后,H5在电脑端都是用input 设置type为 file 来实现文件选择功能,于是才开始搜索资料,发现时需要在webview中设置  setWebChromeClient ,其中有对input 的响应回调:

三、具体实现:

前端代码

<input type="file" accept="*/*" name="choose file">
<input type="file" accept="image/*" name="choose image">
<input type="file" accept="video/*" name="choose video">
<input type="file" accept="image/example" name="take photo and upload image">
<input type="file" accept="video/example" name="take video and upload video">

native端代码:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView,
                                 ValueCallback<Uri[]> filePathCallback,
                                 WebChromeClient.FileChooserParams fileChooserParams) {
    mFilePathCallbacks = filePathCallback;
    // TODO: 根据标签中得接收类型,启动对应的文件类型选择器
    String[] acceptTypes = fileChooserParams.getAcceptTypes();
    for (String type : acceptTypes) {
        Log.d(TAG, "acceptTypes=" + type);
    }
    // 针对拍照后马上进入上传状态处理
    if ((acceptTypes.length > 0) && acceptTypes[0].equals("image/example")) {
        Log.d(TAG, "onShowFileChooser takePhoto");
        Intent it = CameraFunction.takePhoto(mContext);
        startActivityForResult(it, TAKE_PHOTO_AND_UPLOAD_REQUEST);
        return true;
    }

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

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    if (acceptTypes.length > 0) {
        if (acceptTypes[0].contains("image")) {
            intent.setType("image/*");
        } else if (acceptTypes[0].contains("video")) {
            intent.setType("video/*");
        } else {
            intent.setType("*/*");
        }
    } else {
        intent.setType("*/*");
    }

    WebViewActivity.this.startActivityForResult(Intent.createChooser(intent, "File Chooser"),
            REQUEST_FILE_PICKER);
    return true;
}

回调设置uri

/**
 * 设置input 标签出发的回调选择文件路径,优先使用path参数,
 * 其次使用uri参数
 * @param uriParam
 * @param pathParam
 */
private void setFilePathCallback(Uri uriParam, String pathParam) {
    //都为空,则设置null
    if (uriParam == null && pathParam == null) {
        if (mFilePathCallback != null) {
            mFilePathCallback.onReceiveValue(null);
        }
        if (mFilePathCallbacks != null) {
            mFilePathCallbacks.onReceiveValue(null);
        }
    } else if (null != pathParam) { // 优先使用path
        if (mFilePathCallback != null) {
            Uri uri = Uri.fromFile(new File(pathParam));
            mFilePathCallback.onReceiveValue(uri);
        }
        if (mFilePathCallbacks != null) {
            Uri uri = Uri.fromFile(new File(pathParam));
            mFilePathCallbacks.onReceiveValue(new Uri[] { uri });
        }
    } else if (null != uriParam) { //其次使用uri
        if (mFilePathCallback != null) {
            String path = UriUtils.getPath(getApplicationContext(), uriParam);
            Uri uri = Uri.fromFile(new File(path));
            mFilePathCallback.onReceiveValue(uri);
        }
        if (mFilePathCallbacks != null) {
            String path = UriUtils.getPath(getApplicationContext(), uriParam);
            Uri uri = Uri.fromFile(new File(path));
            mFilePathCallbacks.onReceiveValue(new Uri[] { uri });
        }
    }

    mFilePathCallback = null;
    mFilePathCallbacks = null;

}

针对各个请求场景进行处理:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

总结:既然用H5开发APP,就需要了解前端,不懂就要问了。查询方向要对,否则南辕北辙,方向有时候比努力重要!

到此这篇关于关于webview适配H5上传照片或者视频文件的方法的文章就介绍到这了,更多相关webview适配H5上传照片内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

HTML / CSS 相关文章推荐
CSS3 icon font完全指南(CSS3 font 会取代icon图标)
Jan 06 HTML / CSS
用CSS3的box-reflect设置文字倒影效果的方法讲解
Mar 07 HTML / CSS
CSS3制作炫酷带方向感应的鼠标滑过图片3D动画
Mar 16 HTML / CSS
CSS3打造百度贴吧的3D翻牌效果示例
Jan 04 HTML / CSS
纯css实现照片墙3D效果的示例代码
Nov 13 HTML / CSS
css3实现背景模糊的三种方式(小结)
May 15 HTML / CSS
HTML5 Canvas阴影使用方法实例演示
Aug 02 HTML / CSS
HTML5注册页面示例代码
Mar 27 HTML / CSS
如何在Canvas中添加事件的方法示例
May 21 HTML / CSS
Html5实现首页动态视频背景的示例代码
Sep 25 HTML / CSS
详解HTML5常用的语义化标签
Sep 27 HTML / CSS
css3 利用transform-origin 实现圆点分布在大圆上布局及旋转特效
Apr 29 HTML / CSS
浅析HTML5 meta viewport参数
Oct 28 #HTML / CSS
一个基于canvas的移动端图片编辑器的实现
Oct 28 #HTML / CSS
详解HTML5布局和HTML5标签
Oct 26 #HTML / CSS
HTML5实现移动端点击翻牌功能
Oct 23 #HTML / CSS
html5 拖拽及用 js 实现拖拽功能的示例代码
Oct 23 #HTML / CSS
html5小程序飞入购物车(抛物线绘制运动轨迹点)
Oct 19 #HTML / CSS
app内嵌H5 webview 本地缓存问题的解决
Oct 19 #HTML / CSS
You might like
PHP中在数据库中保存Checkbox数据(1)
2006/10/09 PHP
The specified CGI application misbehaved by not returning a complete set of HTTP headers
2011/03/31 PHP
php 判断数组是几维数组
2013/03/20 PHP
Symfony2获取web目录绝对路径、相对路径、网址的方法
2016/11/14 PHP
关于JavaScript的gzip静态压缩方法
2007/01/05 Javascript
uploadify多文件上传参数设置技巧
2015/11/16 Javascript
跟我学习javascript的执行上下文
2015/11/18 Javascript
js编写贪吃蛇的小游戏
2020/08/24 Javascript
微信小程序 Canvas增强组件实例详解及源码分享
2017/01/04 Javascript
Extjs 中的 Treepanel 实现菜单级联选中效果及实例代码
2017/08/22 Javascript
vue实现吸顶、锚点和滚动高亮按钮效果
2019/10/21 Javascript
Nodejs 数组的队列以及forEach的应用详解
2021/02/25 NodeJs
[59:32]Liquid vs Fnatic 2019国际邀请赛淘汰赛败者组BO1 8.20.mp4
2020/07/19 DOTA
Python实现类的创建与使用方法示例
2017/07/25 Python
python DataFrame获取行数、列数、索引及第几行第几列的值方法
2018/04/08 Python
Python将8位的图片转为24位的图片实现方法
2018/10/24 Python
详解Django-restframework 之频率源码分析
2019/02/27 Python
使用Python正则表达式操作文本数据的方法
2019/05/14 Python
Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法
2019/07/04 Python
Python 依赖库太多了该如何管理
2019/11/08 Python
详解Python 重学requests发起请求的基本方式
2020/02/07 Python
Django def clean()函数对表单中的数据进行验证操作
2020/07/09 Python
Flask处理Web表单的实现方法
2021/01/31 Python
使用CSS Grid布局实现网格的流动
2014/12/30 HTML / CSS
localStorage、sessionStorage使用总结
2017/11/17 HTML / CSS
Timex手表官网:美国运动休闲手表品牌
2017/01/28 全球购物
英国折扣零售连锁店:QD Stores
2018/12/08 全球购物
Tommy Hilfiger澳洲官网:美国高端休闲领导品牌
2020/12/16 全球购物
土地租赁意向书
2014/07/30 职场文书
本科应届生求职信
2014/08/05 职场文书
工作作风整顿个人剖析材料
2014/10/11 职场文书
英文慰问信范文
2015/03/24 职场文书
《珍珠鸟》教学反思
2016/02/16 职场文书
react国际化react-intl的使用
2021/05/06 Javascript
PHP中strval()函数实例用法
2021/06/07 PHP
python中的sys模块和os模块
2022/03/20 Python