简体   繁体   English

在webView android中加载本地html文件

[英]Loading local html file in webView android

I have to load an existing html file into a WebView that is located at this path in the file system:我必须将现有的 html 文件加载到位于文件系统中此路径的WebView中:

/data/data/com.example.example/files/file.html

But, when the WebView loads it, I don't see anything.但是,当WebView加载它时,我什么也没看到。 Who can help me?谁能帮我?

WebView code (assuming path is the path I've written above): WebView代码(假设路径是我上面写的路径):

 WebView webView = (WebView)findViewById(R.id.webView1);

  File htmlFile = new File(path);
    if(htmlFile.exists())
    {
        webView.loadUrl(htmlFile.getAbsolutePath());

    }

Try this, adding in a file:/// and doing it a little differently:试试这个,添加一个file:///并做一些不同的事情:

WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///data/data/com.example.example/files/file.html");  

Instead of this, however, you could just put the file into your assets folder in the source code, and then do this:但是,您可以将文件放入源代码中的assets文件夹中,然后执行以下操作:

WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///android_asset/file.html");

The html file should be placed in the assets folder, which will belong in the root directory of your project. html 文件应该放在 assets 文件夹中,该文件夹将位于您项目的根目录中。

So move your file to in case of eclipse因此,将您的文件移动到 eclipse 的情况下

assets/index.html

In an Android Studio project use this folder:在 Android Studio 项目中使用此文件夹:

/app/src/main/assets/index.html

Now use现在使用

WebView wv= (WebView)findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/index.html");

You need to implement a ContentProvider to map local files to uris as explained in this link how to display a local file into Android Webview您需要实现一个 ContentProvider 以将本地文件映射到 uri,如此链接中所述如何将本地文件显示到 Android Webview

or you just load any html page from Assets folder like below:或者您只需从 Assets 文件夹加载任何 html 页面,如下所示:

 WebView wv= (WebView)findViewById(R.id.webView1);
 wv.loadUrl("file:///android_asset/yourfile.html");
 wv.getSettings().setJavaScriptEnabled(true);

Try this:试试这个:

web = (Webview) findViewById(R.id.webview);
web.setWebClient(new WebViewClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setAllowFileAccess(true);
web.getSettings().setAllowFileAccessFromFileURLs(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM