[英]Loading HTML in WebView with custom CSS from non asset folder
I have some CSS
files and JS
files in the /data/data/my.package/files/htmlstuff
directory. 我在/data/data/my.package/files/htmlstuff
目录中有一些CSS
文件和JS
文件。 I also have a bunch of HTML content with me obtained from the network. 我还从网络上获得了一堆HTML内容。 I want to apply these local CSS
and JS
to every html I load in the WebView. 我想将这些本地CSS
和JS
应用于WebView中加载的每个html。 How can I achieve this? 我该如何实现?
If my CSS
and JS
were located in the assets/htmlstuff
directory then I can use the below piece of code: 如果我的CSS
和JS
位于assets/htmlstuff
目录中,那么我可以使用以下代码:
webView.loadDataWithBaseURL("file:///android_asset/htmlstuff/", htmlContent, "text/html", "UTF-8", null);
But how do I achieve the same when the files are present in /files/
directory? 但是,当文件存在于/files/
目录中时,如何实现相同的目的?
The solution to this is the below piece of code: 解决方案是以下代码:
final File filesDir = getContext().getFilesDir();
final File htmlStuffDir = new File(filesDir, "htmlStuff");
final String baseUrlPath = htmlStuffDir.getAbsolutePath() + "/";
webView.loadDataWithBaseURL("file:///" + baseUrlPath, htmlContent, "text/html", "UTF-8", null);
The /
at the end is important and it wouldn't work without that. 末尾的/
很重要,没有它就无法工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.