1

I am trying to view a HTML file, which is stored in my app directory according to scopped storage, The exact HTML file url is "file:///data/user/0/my.app.package/files/Download/page_1.html" However, I am getting open failed: ENOENT (No such file or directory) error. Where I can see manually that the file exist at that path. My target sdk version is 30, Android 11 and I can't change it I need it working on android 11. How can I fix it?

What I have already tried

  1. Added the following code to the manifest file

    android:supportsRtl="true"

    android:usesCleartextTraffic="true"

  2. I am using webviewAssetLoader for the file loading ( working fine with html file from the assets ) but not with file from the app internal storage

Ray
  • 21
  • 5
  • What have you tried to fix the problem yourself? If I google "android webview access denied" there are many StackOverflow questions related to that problem. Maybe this one would be helpful: https://stackoverflow.com/q/57131662/1306012 – Bruno Bieri Aug 16 '21 at 13:27
  • @BrunoBieri Thanks for the comment, however I already did try all the fixes mentioned in the question you tagged. Nothing working. Its due to some policy change in the android-11 API level 30 – Ray Aug 16 '21 at 13:36
  • I see. Then I would highly recommend to update your question with "all the fixes" you've tried already to rule out possible problems. Further I would link the "policy change" you mentioned for Android 11 to further understand that problem. – Bruno Bieri Aug 16 '21 at 13:38
  • Thanks for the advice, I am going to mention the fixes I did try. – Ray Aug 16 '21 at 13:41

3 Answers3

0

I faced the same issue a few weeks ago, you need to change the way to access the HTML file.

You should use the WebViewAssetLoader.

https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader

Grela
  • 96
  • 2
  • Thanks for the answer, it helped to dig more into the WebViewAssetLoader. Now I am using the WebViewAssetLoad as well for viewing the HTML file, but unfortunately now I am getting open failed: ENOENT (No such file or directory) error. Where I can see manually that the file exist at that path. – Ray Aug 17 '21 at 11:41
  • @Ray you could update your question accordingly so other people which try to help you see what you already did. – Bruno Bieri Aug 19 '21 at 07:33
0
binding.webView.apply {
        visible()
        settings.apply {
            useWideViewPort = true
            loadWithOverviewMode = true
            builtInZoomControls = true
            displayZoomControls = false
            allowFileAccess = false
            allowFileAccessFromFileURLs = false
            allowUniversalAccessFromFileURLs = false
            allowContentAccess = true
        }
        val contentUri = FileProvider.getUriForFile(
            this@YourActivity,
            "${BuildConfig.APPLICATION_ID}.provider",
            File(yourHtmlFilePath!!)
        )
        webViewClient = MyWebClient(this@YourActivity)

        contentUri?.let { loadUrl(contentUri.toString()) }
    }

and MyWebClient class is

class MyWebClient(context: Context) : WebViewClientCompat() {
private val assetLoader: WebViewAssetLoader = WebViewAssetLoader.Builder()
    .addPathHandler(
        "/public/", WebViewAssetLoader.InternalStoragePathHandler(
            context,
            File(context.filesDir, "public")
        )
    )
    .build()

override fun shouldInterceptRequest(
    view: WebView,
    request: WebResourceRequest
): WebResourceResponse? {
    return assetLoader.shouldInterceptRequest(request.url)
}

override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
    return if (request.isForMainFrame) {
        view.context.startActivity(
            Intent(Intent.ACTION_VIEW, request.url)
        )
        true
    } else false
}

}

Abdur Rehman
  • 1,067
  • 8
  • 11
0

Just add the below code.

WebSettings settings = mWebView.getSettings();
settings.setAllowFileAccess(true);
Parthi
  • 659
  • 8
  • 25