Android 将本地文件夹中的 html 文件加载到 webview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10223674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading html file from local folder into webview
提问by GAMA
I'm new to Android development.
我是 Android 开发的新手。
I want to load a html fileinto a webview.
我想将html 文件加载到webview 中。
Note that there are so many relevant questions on SO like this, but they all deal with getting **.html* from assetsfolder.
请注意,像这样的SO 上有很多相关问题,但它们都涉及从资产文件夹中获取 **.html* 。
But I want to load html file from local folder, say "D://abc.html" because if my html is around 10Mb then corresponding apk size also goes upto 10mb.
但我想从本地文件夹加载 html 文件,比如“D://abc.html”,因为如果我的 html 大约是 10Mb,那么相应的 apk 大小也会上升到 10mb。
Any help appreciated.
任何帮助表示赞赏。
EDIT
编辑
I tried webView.loadUrl("file:///D:/Projects/myWebsite/index.html");
我试过 webView.loadUrl("file:///D:/Projects/myWebsite/index.html");
but it gives Web page not available
and File not found error
.
但它给出了Web page not available
和File not found error
。
回答by Tony the Pony
You can use:
您可以使用:
WebView webView = // ...
webView.loadUrl("file:///myPath/myFile.html");
In an Android application, files can be read from 3 types of locations:
在 Android 应用程序中,可以从 3 种类型的位置读取文件:
Internal storage:Each app has its own, file names are relative to this location. URL takes form
file:///myFolder/myFile.html
External storage:Needs permission and may not always be available. Get the root folder by calling Environment.getExternalStorageDirectory(). So, construct the URL using:
String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"
Assets: Stored in the apk. Read-only access. URL takes form
file:///android_asset/myFolder/myFile.html
(See also Loading an Android Resource into a WebView)
内部存储:每个应用都有自己的,文件名是相对于这个位置的。URL 采取形式
file:///myFolder/myFile.html
外部存储:需要许可,可能并不总是可用。通过调用Environment.getExternalStorageDirectory()获取根文件夹。因此,使用以下命令构建 URL:
String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"
资产:存储在 apk 中。只读访问。URL 采取形式
file:///android_asset/myFolder/myFile.html
(另请参阅将 Android 资源加载到 WebView 中)
回答by Toni Gamez
In Android 4.4 KitKat, a "Not allowed to load local resource: file:///.." is thrown. arise when loadURL and the only alternative I've found is "loadDataWithBaseURL".
在 Android 4.4 KitKat 中,会抛出“不允许加载本地资源:file:///..”。loadURL 时出现,我发现的唯一替代方法是“loadDataWithBaseURL”。
webView.loadDataWithBaseURL("file:///android_asset/demo/",
tmpDocumentText,"text/html", "UTF-8", null);
回答by RobGThai
WebView has loadData method http://developer.android.com/reference/android/webkit/WebView.html
WebView 有 loadData 方法http://developer.android.com/reference/android/webkit/WebView.html
All you need to do is reading the file into String then feed it to WebView using loadData.
您需要做的就是将文件读入 String,然后使用 loadData 将其提供给 WebView。