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

本文介绍如何在H5应用中实现从手机选择并上传照片或视频到服务器的功能。通过设置WebChromeClient并利用input标签,结合Native端的支持实现跨平台文件上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、需要实现的功能:

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

 

二、分析实现方法:

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

 

三、具体实现:

前端代码

 

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

 

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,就需要了解前端,不懂就要问了。查询方向要对,否则南辕北辙,方向有时候比努力重要!

<think>嗯,用户问的是关于Android WebView内嵌H5视频全屏播放的问题。我需要先回忆一下相关的知识点。首先,WebView默认可能不支持全屏播放,特别是H5视频元素。可能需要配置WebView的一些设置,比如启用JavaScriptDOM存储,可能还需要处理全屏请求的回调。 接下来,用户可能需要具体的步骤,比如如何设置WebChromeClient,重写onShowCustomView方法,处理全屏的进入退出。记得要提到WebView的硬件加速,因为视频播放可能需要这个。 另外,权限问题也很重要,比如在AndroidManifest.xml中声明硬件加速,还有网络权限。H5页面方面,可能需要检查视频标签的属性,比如是否允许全屏,比如playsinline或者webkit-playsinline。 还有全屏时的自定义视图处理,比如隐藏系统UI,设置横屏模式,或者使用自定义的全屏布局。可能还需要处理返回键,退出全屏状态。 需要注意不同Android版本的兼容性,特别是从Android 5.0开始,一些API的变化。另外,第三方浏览器内核可能会有不同的表现,可能需要额外处理。 有没有遗漏的地方?比如,是否要覆盖onHideCustomView来恢复视图?或者如何处理全屏时的方向切换?还有可能提到的常见问题,比如视频不能自动播放,需要用户交互触发。 总之,结构应该是先介绍基本配置,然后详细步骤,包括Java代码示例注意事项,最后可能给出常见问题及解决方法。确保语言简洁,步骤清晰,用户能一步步实现。</think>在 Android WebView 中实现 H5 视频全屏播放,需要处理以下几个关键步骤: ### 一、基础配置 ```java webView.settings.apply { javaScriptEnabled = true // 启用JS domStorageEnabled = true // 启用DOM存储 mediaPlaybackRequiresUserGesture = false // 允许自动播放 setSupportMultipleWindows(true) // 支持多窗口(全屏需要) } ``` ### 二、核心处理类 需自定义 `WebChromeClient` 处理全屏事件: ```java webView.webChromeClient = object : WebChromeClient() { private var customView: View? = null // 处理全屏请求 override fun onShowCustomView(view: View?, callback: CustomViewCallback?) { handleFullscreenEnter(view, callback) } // 退出全屏 override fun onHideCustomView() { handleFullscreenExit() } } ``` ### 三、全屏处理逻辑 ```java private fun handleFullscreenEnter(view: View?, callback: CustomViewCallback?) { // 隐藏系统UI window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // 添加全屏视图 (context as? Activity)?.window?.addContentView( view, FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT) ) customView = view } private fun handleFullscreenExit() { // 移除全屏视图 (customView?.parent as? ViewGroup)?.removeView(customView) customView = null // 恢复系统UI window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE } ``` ### 四、AndroidManifest 配置 ```xml <activity android:name=".MainActivity" android:hardwareAccelerated="true" <!-- 启用硬件加速 --> android:configChanges="orientation|screenSize"/> <!-- 防止页面重载 --> ``` ### 五、H5 页面注意事项 ```html <video controls playsinline <!-- iOS兼容 --> webkit-playsinline <!-- Android兼容 --> style="width:100%; height:auto"> </video> ``` ### 六、常见问题处理 1. **黑屏问题**:检查硬件加速是否开启 2. **方向切换异常**:确保 Activity 配置了 `configChanges` 3. **全屏按钮不显示**:确认视频 `controls` 属性已添加 4. **自动播放失败**:Android 8.0+ 需要用户手势触发 ### 七、高级优化 1. 使用 `FrameLayout` 作为全屏容器 2. 监听返回键退出全屏: ```java override fun onBackPressed() { if (customView != null) { handleFullscreenExit() } else { super.onBackPressed() } } ``` 不同 Android 版本需注意: - Android 5.0+ 需要使用 `onShowCustomView` - 部分第三方内核(X5)需要特殊适配 - 建议最低兼容 API Level 19(Android 4.4)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值