我的应用程序加载位于 getFilesDir()
下的本地 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();
完整示例为 here .但是,更改后它不起作用
targetSdkVersion = 30
.net::ERR_ACCESS_DINIED
android_asset
,则可以加载本地 html 如何在
targetSdkVersion = 30
上加载本地 html 文件?是否更改为被Android FW拒绝?
最佳答案
WebSettings#setAllowFileAccess()
当您的应用程序以 R
为目标时,默认为 false出于安全原因,请注意您需要将 API 级别设置为 30。https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccess(boolean)
关于android - 无法在 API 级别 30 上加载本地 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63128749/