我从网络请求中获取 html fragment 更新,需要将它们插入到本地 html 文件中,然后可以在运行时使用 webView.loadUrl
获取该文件。
我可以使用 webView.loadUrl
轻松地从 /android_assets/myFile.html
加载文件,但我无法写入文件,因为 /android_assets
目录在运行时不可访问:Writing to /android_assets at runtime
所以我的问题是,是否有另一个位置可以放置 myFile.html
,以便我可以在运行时写入它并将其加载到 webView
中?
最佳答案
为此,您应该从 html 加载内容,修改它,并将其保存到存储中。
Usage: String yourData = LoadData("myFile.html");
public String LoadData(String inFile) {
String tContents = "";
try {
InputStream stream = getAssets().open(inFile);
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
tContents = new String(buffer);
} catch (IOException e) {
// Handle exceptions here
}
return tContents;
}
在 WebView 中加载数据。调用WebView的loadData()方法
webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");
关于android - 在运行时写入 android Assets 目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52048266/