i have created a webview app for android studio. but didn't load the web url. the error is net::ERR_ACCESS_DENIED. can anyone help with this
-
What android version are you using?– lellomanCommented Jul 21, 2019 at 9:54
-
@lelloman 3.4.2– AnuradhaCommented Jul 21, 2019 at 9:56
-
3weird, I'm facing the same issue but only on android 10, up to android 9 is fine :\– lellomanCommented Jul 21, 2019 at 10:06
-
1@tophernuts yes, in the end it was probably remembering that it didn't have permission from a previous launch, I just un-deleted my answer, you can find it below.– lellomanCommented Oct 2, 2019 at 7:49
-
5@tophernuts don't know, anyway it was: For me the problem was a bit silly, I had originally forgot to add android:usesCleartextTraffic="true" in the manifest and launched the app. After I added android:usesCleartextTraffic="true" it still gave ERR_ACCESS_DENIED. I then cleared the data, un-installed the app, re-installed it and bam, the error is gone.– lellomanCommented Oct 17, 2019 at 5:28
11 Answers
For me the problem was a bit silly, I had originally forgot to add android:usesCleartextTraffic="true"
in the manifest and launched the app. After I added android:usesCleartextTraffic="true"
it still gave ERR_ACCESS_DENIED
. I then cleared the data, un-installed the app, re-installed it and bam, the error is gone.
Try add these to your webview code:
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
If it still doesn't work, try add android:usesCleartextTraffic="true"
to your application tag in your manifest.
-
1Adding only
setAllowFileAccess(true)
solved it for me (Android 7.0).– Mahm00dCommented Dec 20, 2021 at 13:01
Just started to develop android apps and got the same error even when I had permission:
<uses-permission android:name="android.permission.INTERNET"/>
Then I've uninstalled the app inside emulator and build again and now the app can access internet.
In my case, it happened after upgrading my app to SDK 30. And the solution was:
WebSettings settings = webView.getSettings();
settings.setAllowFileAccess(true);
The default setting was true before but with SDK 30 it's false. So you explicitly need to allow access.
Check if you have provided Internet Permission :
<uses-permission android:name="android.permission.INTERNET"/>
and put this in Manifest :
<application
android:usesCleartextTraffic="true">
</application>
-
3
Here is a more likely scenario (than the one in the accepted answer) in which this error may appear if you're trying to access a file using file://
in WebView
.
The default value of setAllowFileAccess
was changed from true
to false
from Android 11.
The default value is true for apps targeting
Build.VERSION_CODES.Q
and below, and false when targetingBuild.VERSION_CODES.R
and above.
This means you'll now have to set it to true
manually.
webView.getSettings().setAllowFileAccess(true);
Official docs for more info.
Remember if you need internet, you'll need to set the permission in the manifest, in the top add the Tag uses-permission like this
<uses-permission android:name="android.permission.INTERNET"/>
uninstalling and then reinstalling worked for me.
-
1Please not that
[...] worked for me
is not really an answer to the question as every system works different. I would rather add that as a comment or expand your explanation.– DominikCommented Mar 9, 2021 at 13:59
I also wanted to add my observations on file access denied.
1. It can happens when you try to used files from storage i.e. internal memory or external memory.
Here you can do-
a)Declare tags in the manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
b) First you need to put these two lines when working with webview
webView.settings.allowContentAccess = true
webView.settings.allowFileAccess = true
In the last line of the webview you must use below statement
webView.loadUrl("file///....")
This is because, when you tried to loadUrl before allowContentAccess and
allowFileAccess it is not able to read the storage. So, first allow access
and the use loadurl. it will work perfectly fine every time.
2. When you are working with online urls-
a) Put <uses-permission android:name="android.permission.INTERNET"/>
in manifest.
b) Use android:usesCleartextTraffic="true"
inside application tag in manifest.
c) Also this snippet need to be added-
webView.settings.allowContentAccess = true
webView.settings.allowFileAccess = true
webView.loadUrl("file///....")
Hope your code works. Cheers :)
If not basic permissions, this can also happen if you're trying to load SSL encrypted webpage (starting with https://) and it throws any error. To take care of that, you can add:
// Ignoring SSL certificate errors
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
And for some trying to load local file, it also happened as the project lack read permission, I'm not sure why, but you can try adding this to your manifest also:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Or, either you can use Android Smart WebView framework that can take care of all such errors on it's own.
- Provide permission in Android Manifest.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2.Add android:usesCleartextTraffic="true"
in <application tag in AndroidManifest File.
3.Add this Two Line in Class File where your WebView.
secondwebView.settings.allowContentAccess = true
secondwebView.settings.allowFileAccess = true
Here is My Code. `
package com.example.webview
class MainActivity : AppCompatActivity() {
lateinit var webView: WebView
lateinit var progressDialog:ProgressDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.firstwebview)
setUpMap()
Handler().postDelayed(
{
progressDialog.dismiss()
},2000)
}
@SuppressLint("SetJavaScriptEnabled")
private fun setUpMap() {
progressDialog = ProgressDialog(this)
progressDialog.setMessage("wait for loading")
progressDialog.setCancelable(false)
progressDialog.show()
webView.settings.allowContentAccess = true
webView.settings.allowFileAccess = true
webView.settings.javaScriptEnabled = true
webView.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
webView.loadUrl(
"file:///android_asset/index.html"
)
webView.webChromeClient = object:WebChromeClient(){
override fun onJsAlert(
view: WebView?,
url: String?,
message: String?,
result: JsResult?
): Boolean {
return super.onJsAlert(view, url, message, result)
}
}
webView.webViewClient = object : WebViewClient(){
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
}
}
}
}