question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to load local file on Android

See original GitHub issue

When trying to open a local webpage from tyhe local file system (using file://) it works on iOS, but on Android we get an error:

Encountered an error loading page:
{
  canGoBack: false,
  canGoForward: false,
  code: -1,
  description: "net::ERR_ACCESS_DENIED",
  loading: false,
  target: 165,
  title: "Webpagina niet beschikbaar",
  url: "file:///data/user/0/com.twipepocv4/cache/data/36/Page-313.html",
}

This is my code:

      <WebView
        originWhitelist={['*']}
        allowFileAccess={true}
        source={{uri: "file:///data/user/0/<myapp>/cache/data/36/Page-319.html"}}
        domStorageEnabled={true}
        allowUniversalAccessFromFileURLs={true}
        allowFileAccessFromFileURLs={true}
      />

I believe this has something to do with AllowFileAccessFromFileURLs but I can’t get it to work. Should any other permission be set?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:28 (1 by maintainers)

github_iconTop GitHub Comments

49reactions
hsourcecommented, Nov 7, 2019

@tjensen is exactly right on the cause. Here’s a more detailed investigation and a workaround that we use. This only applies to Android 10.

Problem

react-native-webview on Android handles the allowFileAccess and allowUniversalAccessFromFileURLs by calling view.getSetings().setAllowFileAccess() in the Java code: https://github.com/react-native-community/react-native-webview/blob/47e9a0b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java#L484

At the same time, setting source calls view.loadUrl(). This results in a race where if view.loadUrl() executes before the security settings are updated, the page-load will fail.

Workaround

We don’t load your local file until we’re sure the correct permissions have been set on the WebView.

On the component you have that renders <WebView source={...}>:

  1. Create a state variable in the constructor or the class
this.state = { renderedOnce: false };
  1. In componentDidMount, set that variable to be true
componentDidMount() {
  this.setState({ renderedOnce: true });
}
  1. When rendering, render your file only when renderedOnce is true:
render() {
  return <WebView source={renderedOnce ? { uri: indexHtmlPath } : undefined} />;
}
40reactions
mayursarode4commented, Jul 4, 2019

You guys are not doing properly, Proper ways is as follows: First install it using (If not installed), npm install --save react-native-webview // inside project root directory react-native link react-native-webview // it will link WebView with iOS and Android

import React, { Component } from 'react';
import { WebView } from 'react-native-webview';
import {LocalHtml} from './project-root/html/demo.html'; // path of your file starts with "./"
_// as per above example replace it with above LocalHtml_
_//import {LocalHtmlText} from './data/user/0/com.twipepocv4/cache/data/36/Page-313.html';_

class MyInlineWeb extends Component {
  render() {
    return (
      <WebView
        originWhitelist={['*']}
        source={{ html: LocalHtml }} // here it will render your html content and load on webview.
        javaScriptEnabled={true}
        domStorageEnabled={true}
        useWebKit={true}
        startInLoadingState={true}

      />
    );
  }
}

I hope this will help you guys.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to load local html file on API Level 30 - Stack Overflow
to show local html files in webView in api level 30, I used this code binding.webView.apply { visible() settings.apply { useWideViewPort ...
Read more >
Can't open a Google file or folder - Android
On your Android device, open the app for Google Drive, Docs, Sheets, or Slides. At the top left, tap Menu Menu . Next...
Read more >
Why can't Chrome read a local HTML file on Android? - Quora
You have to grant Chrome permission to access the Android File System. Then opening a html page including javascript, css and images should...
Read more >
Android can't view local content after OS upgrade
If I run Files and view the Downloads folder, for example, it merely says "Can't load content at the moment". I've been using...
Read more >
Loading in-app content - Android Developers
You can provide web-based content—such as HTML, JavaScript, ... Download the Android_symbol_green_RGB.png file to your local machine.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found