[英]Android: read html files from external folder and display in webview
I want to display html files in webview. 我想在webview中显示html文件。 The html files are placed under this folder of my tab, "file manager/Device storage/Android/webpages. With this code I'm getting null pointer exception probably I am not passing the path correctly. And the second problem I am not getting how to load the webpage in webview. html文件放在我的选项卡的这个文件夹下,“文件管理器/设备存储/ Android /网页。这个代码我得到空指针异常可能我没有正确地传递路径。而第二个问题我没有得到如何在webview中加载网页。
WebView webView = new WebView(this);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//webView.loadUrl("file:///android_asset/K1.html");
//setContentView(webView);
File folder = new File("/FileManager/Home/Devicestorage/Android/webpages");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if(listOfFiles[i].getName().endsWith(".html")){
System.out.println("File " + listOfFiles[i].getName());
}
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
Don't know if we can open a file with an absolute path on Android like you did. 不知道我们是否可以像你一样在Android上打开一个绝对路径的文件。 Here's what I did: 这是我做的:
File extDir = context.getExternalFilesDir(null);
String root = extDir.toString();
File myFile = new File(root + "/your_path/your_file");
... open myFile for reading ...
and you would need the external storage permission in the manifest. 并且您需要清单中的外部存储权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.