当前位置: 首页 > news >正文

Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)

webview打开本地Html文件

1.在路径前面加上file://

            String filePath="file://"+path;webView.loadUrl( filePath);

2.打开权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

3.启用JavaScript 设置本地访问权限

 webView.getSettings().setJavaScriptEnabled(true);webSettings.setAllowContentAccess(true);

4.如果js代码访问本地文件出现CORS跨源请求 可以通过重写WebViewClient的shouldInterceptRequest函数拦截请求并替换为本地资源

 webView.setWebViewClient(new WebViewClient(){@Nullable@Overridepublic WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {// 获取请求的URLString url = null;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {url = request.getUrl().toString();}// 检查URL是否符合你想要替换的file域规则if (url.startsWith("file://")) {String path=url;try {if(path.contains(".jpg")||path.contains(".png")){Map<String, String> headers = new HashMap<>();headers.put("Access-Control-Allow-Origin", "*");InputStream inputStream = new FileInputStream(new File(path));if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {WebResourceResponse response= new WebResourceResponse("image/png", "UTF-8", inputStream);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {response.setResponseHeaders(headers);}return response;}}else {} catch (IOException e) {// 处理异常e.printStackTrace();}}// 如果不是file域请求,则返回null继续加载return super.shouldInterceptRequest(view, request);}});

5.如果H5项目中用到了Fetch 出现报错Fetch API cannot load 是因为Fetch不支持使用file://访问本地文件, 可以在html文件中处理下
如果是 arraybuffer 可以这样处理

<script>window.fetch=function fetchLocal(url, data) {return new Promise(function (resolve, reject) {var xhr = new XMLHttpRequestxhr.onload = function () {resolve(new Response(xhr.response, { status: xhr.status }))}xhr.onerror = function () {reject(new TypeError('Local request failed'))}xhr.open('GET', url)xhr.responseType = "arraybuffer";xhr.send(null)})
}</script>

否则可以这样处理下

<script>window.fetch=function fetchLocal(url, data) {return new Promise(function(resolve, reject) {var xhr = new XMLHttpRequestxhr.onload = function() {resolve(new Response(xhr.responseText, {status: xhr.status}))}xhr.onerror = function() {reject(new TypeError('Local request failed'))}xhr.open('GET', url)xhr.send(data)})
}</script>

至此,就可以离线访问本地的Unity,Cocos等H5游戏

http://www.lryc.cn/news/474184.html

相关文章:

  • 解决项目中图片出不来的bug
  • 手机实时提取SIM卡打电话的信令声音-新的篇章(三、Android虚拟声卡探索)
  • REST APIs与微服务:关键差异
  • 【网安案例学习】反向蛮力攻击Reverse Brute Force Attack
  • TCP/IP网络编程:理解网络编程和套接字
  • CSS实现回到顶部且平滑过渡
  • 10 go语言(golang) - 数据类型:哈希表(map)及原理(二)
  • 【论文解读】Med-BERT: 用于疾病预测的大规模结构化电子健康记录的预训练情境化嵌入
  • [POI2014] PTA-Little Bird(单调队列优化 DP)
  • 【含开题报告+文档+PPT+源码】基于SpringBoot的体育馆管理系统的设计与实现
  • Vue3学习:vue组件中的图片路径问题
  • openCV基础-图像预处理Day26
  • 给文件添加可读可写可执行权限
  • golang有序map
  • 【LangChain系列4】【Chain模块详解】
  • 51c嵌入式~IO合集1
  • ETLCloud怎么样?深度解析其在数据管理中的表现
  • 高频谐振功放电路
  • kafka如何获取 topic 主题的列表?
  • 全新大模型框架Haystack,搭建RAG pipeline
  • 儿童孤独症专家分享:了解治疗与支持的专业帮助
  • 初始JavaEE篇——多线程(7):定时器、CAS
  • 高精度计算(乘)
  • 在vue中 如何实现跨域
  • 计算机考研,选择西安交通大学还是哈工大?
  • 微积分复习笔记 Calculus Volume 1 - 4.4 The Mean Value Theorem
  • Cpp多态机制的深入理解(20)
  • (六)Python结构数据类型
  • C++进阶-->多态(Polymorphism)
  • python实战项目51:selenium结合requests获取某众点评评论