从本地文件夹到的WebView加载HTML文件 [英] Loading html file from local folder into webview

查看:158
本文介绍了从本地文件夹到的WebView加载HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid的发展。

I'm new to Android development.

我要加载的一个HTML文件的成的一个web视图的。

请注意,有上这么喜欢这样,但他们都处理得到*的的.html 的距离的资产的文件夹。

Note that there are so many relevant questions on SO like this, but they all deal with getting *.html from assets folder.

不过,我想从本地文件夹加载HTML文件,说。D://abc.html,因为如果我的HTML大约为10Mb则对应的APK尺寸也变为高达10MB

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.

任何帮助AP preciated。

Any help appreciated.

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

但它给不可用网​​页未找到文件错误

推荐答案

您可以使用:


   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

外部存储:需要许可,不得始终可用。获取根文件夹通过调用<一href="http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()">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)

这篇关于从本地文件夹到的WebView加载HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆