WebView does not load local HTML file

I’m attempting to load a local HTML file to webview and sometimes does not find the file and sometimes does not have access.

This is the error ERR_ACCESS_DENIED
or ERR_FILE_NOT_FOUND

This is the code of the app.js if it works:

`/**

import React, {Component} from ‘react’;

import { StyleSheet, Text, View, Button,Platform } from “react-native”;

import { WebView } from ‘react-native-webview’;
import HTML_FILE from “./AdListing/index.html”
const isAndroid= Platform.OS===’android’;

export default class App extends Component {
constructor(props) {
super(props);
this.state = { val: 1 };
}
renderElement() {
if (this.state.val === 1) {
return (
<WebView
style={{flex: 1}}
originWhitelist={[‘‘]}
source={{ uri: “https://aboutreact.com” }}
style={{ marginTop: 20 }}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
);
} else if (this.state.val === 2) {
return (
<WebView
style={{flex: 1}}
originWhitelist={[‘
‘]}
source={{ html: ‘

Hello

‘ }}
style={{ marginTop: 20 }}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
);
} else {
if(isAndroid){
return (
<WebView
style={{flex: 1}}
originWhitelist={[‘‘]}
source={{uri:’file:///android_assets/index.html’}}
style={{ marginTop: 20 }}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
)
}else{
return(
<WebView
style={{flex: 1}}
originWhitelist={[‘
‘]}
source={HTML_FILE}
style={{ marginTop: 20 }}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
);
}
}
}
render() {
return (
<View style={{flex: 1, marginTop:30, backgroundColor:’white’}}>
<Button title=”Load from URL” onPress={() => this.setState({ val: 1 })}/>
<Button title=”Load HTML Text” onPress={() => this.setState({ val: 2 })}/>
<Button title=”Load from Local HTML FIle” onPress={() => this.setState({ val: 3 })}/>
<View style={{flex: 1}}>
{this.renderElement()}

);
}
}`

I want to show the local html file in the project but I cannot or I don’t know, please help! Thanks

Environment:

  • OS: Android
  • OS version: 9.0
  • react-native version: 16.8.3
  • react-native-webview version: 5.11.0

3 thoughts on “WebView does not load local HTML file