HarmonyOS Web加载的网页怎么允许读取app沙箱路径下的文件

Web组件加载完成了网页,此时网页内要读取app沙箱路径下的文件,读取是通过访问这串链接进行的file://data/storage/el2/base/haps/entry/files/1719395161522.jpg

但此时js里抛出一个错误:Not allowed to load local resource: file://data/storage/el2/base/haps/entry/files/1719395161522.jpg

如何加载正确web端将显示沙箱里的这张图片

HarmonyOS
2024-12-27 16:00:48
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

可以通过转图片为base64格式后再用h5加载

//Demo.ets:

import { common } from '@kit.AbilityKit';
import { util } from '@kit.ArkTS';
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct Demo {
  webController: webview.WebviewController = new webview.WebviewController();
  @State test: Test = new Test()
  build() {
    Column(){
      Web({src:$rawfile('index2.html'),controller:this.webController})
        .javaScriptProxy({
          object: this.test,
          name: "picture",
          methodList: ["takePhoto"],
          controller: this.webController
        }).height("50%")
    }
  }
}
class Test{
  takePhoto() {
    let context = getContext(this) as common.UIAbilityContext
    let arrayBuff = context.resourceManager.getRawFileContentSync("startIcon.png")
    let base64helper = new util.Base64Helper()
    let base64string = base64helper.encodeToStringSync(arrayBuff,util.Type.MIME)
    console.debug(base64string)
    return base64string
  }
}
//index.html:

<!DOCTYPE html>
  <html lang="zh-CN">
  <head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <style>
    .disable-longpress {
  user-select: none; /* 禁止文本选择 */
}
</style>
  <body>
  <button id="init" onclick="takePicture()">模拟拍照</button>
  <img src="" id="myImage" alt="">
  </body>
  </html>
  <script>
  function takePicture() {
    let s = 'data:image/png;base64,' + picture.takePhoto();
    console.log(s)
    document.getElementById('myImage').src =  s;
  }
  </script>
分享
微博
QQ
微信
回复
2024-12-27 17:58:46
相关问题
HarmonyOS 预览沙箱路径文件失败
322浏览 • 1回复 待解决
沙箱路径文件怎么拿取?
2087浏览 • 1回复 待解决
如何读取读取分布式路径文件
731浏览 • 1回复 待解决
修改沙箱路径json文件指定内容
2643浏览 • 1回复 待解决
HarmonyOS web加载沙箱文件失败
418浏览 • 1回复 待解决
ETs,对沙箱路径json文件内容遍历
3532浏览 • 1回复 待解决
怎么读取sdcardtxt文件
10286浏览 • 2回复 待解决
HarmonyOS Web加载网页白屏
936浏览 • 1回复 待解决
HarmonyOS web从本地沙箱加载
574浏览 • 1回复 待解决