gpt4 book ai didi

android - 将本地文件夹中的 html 文件加载到 webview 中

转载 作者:IT老高 更新时间:2023-10-28 22:16:41 33 4
gpt4 key购买 nike

我是 Android 开发新手。

我想将一个html文件加载到一个webview中。

请注意,关于 SO 的相关问题非常多,例如 this ,但它们都处理从 assets 文件夹中获取 **.html* 的问题。

但我想从本地文件夹加载 html 文件,比如“D://abc.html”,因为如果我的 html 大约为 10Mb,那么相应的 apk 大小也会达到 10mb。

任何帮助表示赞赏。

编辑

我试过 webView.loadUrl("file:///D:/Projects/myWebsite/index.html");

但它给出了 Web page not availableFile not found error

最佳答案

你可以使用:


WebView webView = // ...

webView.loadUrl("file:///myPath/myFile.html");

在 Android 应用程序中,可以从 3 种类型的位置读取文件:

  • 内部存储:每个应用都有自己的,文件名是相对于这个位置的。 URL 采用 file:///myFolder/myFile.html

  • 形式
  • 外部存储:需要许可,并且可能并不总是可用。调用Environment.getExternalStorageDirectory()获取根目录.因此,使用以下方法构造 URL:String url = "file:///"+ Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"

  • Assets :存储在 apk 中。只读访问。 URL 采用 file:///android_asset/myFolder/myFile.html 形式(另见 Loading an Android Resource into a WebView)

关于android - 将本地文件夹中的 html 文件加载到 webview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10223674/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com