34

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Anuradha
  • 341
  • 1
  • 3
  • 7
  • What android version are you using? – lelloman Jul 21 '19 at 09:54
  • @lelloman 3.4.2 – Anuradha Jul 21 '19 at 09:56
  • That version doesn't exist, you can check that in the settings of your phone, inside an entry like "about your phone" – lelloman Jul 21 '19 at 10:00
  • @lelloman android version is 9. android studio version is 3.4.2 – Anuradha Jul 21 '19 at 10:04
  • 3
    weird, I'm facing the same issue but only on android 10, up to android 9 is fine :\ – lelloman Jul 21 '19 at 10:06
  • is the url `http` or `https`?. _"From Android 9 Pie now, requests without encryption will never work."_. see https://stackoverflow.com/questions/51902629/how-to-allow-all-network-connection-types-http-and-https-in-android-9-pie. though I'm not sure about web view – O-9 Jul 21 '19 at 10:41
  • @lelloman I'm seeing this as well, only on Android 10 when trying to upload a file to S3 from a webview. Did you ever find any solutions? – tophernuts Oct 01 '19 at 19:01
  • 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. – lelloman Oct 02 '19 at 07:49
  • @lelloman I'm not able to see your answer here, I'm not sure if it's a weird stackoverflow thing? – tophernuts Oct 16 '19 at 20:22
  • 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. – lelloman Oct 17 '19 at 05:28

11 Answers11

40

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.

lelloman
  • 13,883
  • 5
  • 63
  • 85
22

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.

mindrex
  • 239
  • 2
  • 2
15

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.

HasanG
  • 12,734
  • 29
  • 100
  • 154
15

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.

Fatih
  • 507
  • 4
  • 15
9

Check if you have provided Internet Permission :

<uses-permission android:name="android.permission.INTERNET"/>

and put this in Manifest :

<application
    android:usesCleartextTraffic="true">
</application>
Ashish
  • 6,791
  • 3
  • 26
  • 48
3

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"/>
HeinerTheBest
  • 201
  • 2
  • 3
2

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 targeting Build.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.

Divins Mathew
  • 2,908
  • 4
  • 22
  • 34
1

uninstalling and then reinstalling worked for me.

Anup Yadav
  • 19
  • 4
  • 1
    Please 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. – Dominik Mar 09 '21 at 13:59
1

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 :)

  • You saved my life. I had everything fine. the only issue was loadUrl() was written before websettings properties – R. Rohilla May 11 '23 at 11:40
0

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.

Ghazi
  • 176
  • 1
  • 11
0
  1. 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)
        }
    }

}

}