'ERR_ACCESS_DENIED in webView in android
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
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
Check if you have provided Internet Permission :
<uses-permission android:name="android.permission.INTERNET"/>
and put this in Manifest :
<application
android:usesCleartextTraffic="true">
</application>
Solution 6:[6]
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"/>
Solution 7:[7]
uninstalling and then reinstalling worked for me.
Solution 8:[8]
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.
Solution 9:[9]
- 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)
}
}
}
}
Solution 10:[10]
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 :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
