要保存HTML并在Android中读取,您可以使用以下步骤和示例代码:
String htmlContent = "Hello, World!
";
String fileName = "example.html";
try {
File file = new File(context.getFilesDir(), fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(htmlContent.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
WebView webView = findViewById(R.id.webView);
webView.loadUrl("file://" + context.getFilesDir() + "/" + fileName);
通过上述步骤,您可以成功保存HTML并在Android中加载和显示它。请注意,要访问外部存储(如SD卡)上的HTML文件,您需要添加适当的权限到AndroidManifest.xml文件中:
但是,在Android 10及更高版本中,您无法直接访问外部存储。相反,您应该使用媒体存储API(MediaStore API)来保存和读取HTML文件。
上一篇:保存HTML表格中的数据
下一篇:保存HTML页面的DOM树