简体   繁体   English

Android Studio(网页帮助)网:ERR_FILE_NOT_FOUND

[英]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 : 我试图从本地文件在android studio中实现我的Web应用程序/页面,我按照官方文档在xml中创建了webview按钮,并声明:

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

in the manifest file. 在清单文件中。 My java activity code is below : 我的Java活动代码如下:

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 是我的HTML文件是JS文件

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/..." 将Web资源放入资产文件夹中,通常位于<project>/src/main/assets ,因为仿真器无法在您的计算机根目录".../C:/Users/olive/OneDrive/Documents/ComputerScience/..."下看到文件".../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: 请记住,在Android应用程序中,可以从3种类型的位置读取文件:

  • Internal storage : Each app has its own, file names are relative to this location. 内部存储空间 :每个应用程序都有其自己的文件名,是相对于此位置的。 URL takes form file:///myFolder/myFile.html URL格式为file:///myFolder/myFile.html
  • External storage : Needs permission and may not always be available. 外部存储设备 :需要许可,可能并不总是可用。 Get the root folder by calling Environment.getExternalStorageDirectory(). 通过调用Environment.getExternalStorageDirectory()获得根文件夹。 So, construct the URL using: String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html" 因此,请使用以下内容构造URL:字符串url =“ file:///” + Environment.getExternalStorageDirectory()。toString()+ File.separator +“ myFolder / myFile.html”
  • Assets : Stored in the apk. 资产 :存储在apk中。 Read-only access. 只读访问。 URL takes form file:///android_asset/myFolder/myFile.html (See also Loading an Android Resource into a WebView) URL的格式为file:///android_asset/myFolder/myFile.html(另请参阅将Android资源加载到WebView中)

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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