我的应用程序加载位于getFilesDir()via下的本地 html 文件WebView#loadUrl()。
之前targetSdkVersion = 29,下面的代码正在运行。
copyAssetsFile(this, "sample.html", getFilesDir().getAbsolutePath());
webView.getSettings().setJavaScriptEnabled(true);
String url = "file://" + getFilesDir().getAbsolutePath() + "/sample.html";
webView.loadUrl(url);
}
private static void copyAssetsFile(Context context, String fileName, String directoryPath) {
try {
InputStream inputStream = context.getResources().getAssets().open(fileName);
FileOutputStream fileOutputStream = new FileOutputStream(
new File(directoryPath, fileName), false);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = inputStream.read(buffer)) >= 0) {
fileOutputStream.write(buffer, 0, length);
}
fileOutputStream.close();
inputStream.close();
Run Code Online (Sandbox Code Playgroud)
完整示例在这里。
但是,它在更改后不起作用targetSdkVersion = 30 …