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
.
net::ERR_ACCESS_DINIED
android_asset
How to load local html file on targetSdkVersion = 30
?
Is it changed to be denied by Android FW??
WebSettings#setAllowFileAccess()
is default to false when your app is targeting R
and above for security reasons, note that you need to set the API level 30. https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccess(boolean)
Try setting webview.getSettings().setAllowFileAccess(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With