且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从本地文件夹到的WebView加载HTML文件

更新时间:2022-06-15 08:43:04

您可以使用:


   WebView webView = // ...

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

在一个Android应用程序,文件可以从3种类型的存储单元,读:

In an Android application, files can be read from 3 types of locations:

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

  • Internal storage: Each app has its own, file names are relative to this location. URL takes form file:///myFolder/myFile.html

外部存储:需要许可,不得始终可用。获取根文件夹通过调用Environment.getExternalStorageDirectory(). 字符串URL =:那么,用构造URL的file:///+ Environment.getExternalStorageDirectory()的toString()+文件分割符+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"

资产:存储在APK。只读访问。 URL的形式文件:///android_asset/myFolder/myFile.html (另见的加载一个Android资源到的WebView

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)