打开资产文件夹中的 HTML 个文件

Open HTML file from assets folder

我正在尝试在我的 Android 应用程序中打开本地 html 文件。

该文件位于我的资产文件夹下。所以我正在设置一个 WebViewClient 并将我的页面加载到其中。 但是我收到 "webpage not available" 错误。

这是我的 Activity 代码:

public class LocalDialogActivity extends Activity {

    protected WebView webView;
    private static final String ENROLLMENT_URL = "file:///assets/enrollment.html";

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

        webView = (WebView)findViewById(R.id.local_dialog_webview);
        webView.setWebViewClient(new WebViewClient());

        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);

        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);


        refreshWebView(webView);

    }

    public void refreshWebView(View view) {
        webView.loadUrl(ENROLLMENT_URL);
    }

和布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView
        android:id="@+id/local_dialog_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

我的 AndroidMainfest.xml 文件中有互联网权限,可以访问互联网:

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

欢迎任何帮助。 谢谢。

此代码有效

public class WebActivity extends Activity {
  WebView wv;

 String url="file:///android_asset/sample.html";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);
    wv=(WebView)findViewById(R.id.webUrl_WEB);



WebSettings webSettings = wv.getSettings();
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    wv.getSettings().setBuiltInZoomControls(true);
    wv.getSettings().setPluginState(PluginState.ON);


    wv.setWebViewClient(new myWebClient());

    wv.loadUrl(url);
}




public class myWebClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }
}

您需要拥有 AndroidMainfest.xml 可以访问互联网的文件的权限:

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

您需要将 sample.html 文件放在资产文件夹下

尝试使用下面的代码加载 html

"file:///android_asset/enrollment.html" 

而不是

"file:///assets/enrollment.html"

如果你的结构应该是这样的:

/assets/html/index.html

/assets/scripts/index.js

/assets/css/index.css

然后就做 ( Android WebView: handling orientation changes )

    if(WebViewStateHolder.INSTANCE.getBundle() == null) { //this works only on single instance of webview, use a map with TAG if you need more
        webView.loadUrl("file:///android_asset/html/index.html");
    } else {
        webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());
    }

确保添加

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
    }

然后只需使用 urls

<html>
<head>
    <meta charset="utf-8">
    <title>Zzzz</title>
    <script src="../scripts/index.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/index.css">

我认为,最好使用"raw"文件夹。该代码可以正常工作。

 InputStream is = getResources().openRawResource(R.raw.html_file);

     try {
         int size = is.available();
         byte[] buffer = new byte[size];
         is.read(buffer);
         is.close();
         String str = new String(buffer);
         }catch (IOException e){
            e.printStackTrace();
         }
         webView.loadDataWithBaseURL("", str, "text/html", "UTF-8", "");

从这里下载源代码 (Open html file from assets android)

MainActivity.java

package com.deepshikha.htmlfromassets;
 import android.app.ProgressDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

WebView webview;
 ProgressDialog progressDialog;

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

private void init(){
 webview = (WebView)findViewById(R.id.webview);
 webview.loadUrl("file:///android_asset/download.html");
 webview.requestFocus();

progressDialog = new ProgressDialog(MainActivity.this);
 progressDialog.setMessage("Loading");
 progressDialog.setCancelable(false);
 progressDialog.show();

webview.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url) {
 try {
 progressDialog.dismiss();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 });
 }
 }

谢谢!