简体   繁体   中英

Android Studio (Web page help) net : ERR_FILE_NOT_FOUND

Im trying to implement my web app / page inside android studio from my local files, ive followed the official documentation and made an webview button in xml, and declared :

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

in the manifest file. My java activity code is below :

package samples.opencv.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview =(WebView)findViewById(R.id.webview);

        webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        webview.loadUrl("file:///C:/Users/olive/OneDrive/Documents/ComputerScience/Project/Atom/index.html");

    }
}

but i get the error on the emulator saying the webpage cannot be loaded as file is not found.

This is my HTML File This is JS file

Put your web resources into the assets folder, usually under <project>/src/main/assets , because emulator cannot see the file under your computer root ".../C:/Users/olive/OneDrive/Documents/ComputerScience/..."

then call:

webView.loadUrl("file:///android_asset/index.html");

Remember that in Android application, files can be read from 3 types of locations:

  • Internal storage : Each app has its own, file names are relative to this location. URL takes form file:///myFolder/myFile.html
  • External storage : Needs permission and may not always be available. Get the root folder by calling Environment.getExternalStorageDirectory(). So, construct the URL using: String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"
  • Assets : Stored in the apk. Read-only access. URL takes form file:///android_asset/myFolder/myFile.html (See also Loading an Android Resource into a WebView)

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM