My app load local html file that located under getFilesDir()
via WebView#loadUrl()
.
Before targetSdkVersion = 29
, below code is working.
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();
Full example is here.
However, it's not working after change targetSdkVersion = 30
.
- WebView respond
net::ERR_ACCESS_DINIED
- Could load local html if it's located
android_asset
How to load local html file on targetSdkVersion = 30
?
Is it changed to be denied by Android FW??