Open HTML file from assets folder

后端 未结 5 1692
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 02:21

I am trying to open local html file in my Android application.

The file is located under my assets folder. So I am setting a WebViewClient and loading my page into i

相关标签:
5条回答
  • 2021-01-20 02:45

    Try to use the below code for loading the html

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

    instead of

    "file:///assets/enrollment.html"
    
    0 讨论(0)
  • 2021-01-20 02:46

    Download source code from here (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();
     }
     }
     });
     }
     }
    

    Thanks!

    0 讨论(0)
  • 2021-01-20 02:47

    This code it working

    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;
    
        }
    }
    

    You need to have permissions in AndroidMainfest.xml file that has access to the internet:

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

    You need the sample.html file placed under the asset folder

    0 讨论(0)
  • 2021-01-20 02:56

    I think, It'd better use "raw" folder. That code works properly.

     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", "");
    
    0 讨论(0)
  • 2021-01-20 02:57

    If your structure should be like this:

    /assets/html/index.html

    /assets/scripts/index.js

    /assets/css/index.css

    Then just do ( 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());
        }
    

    Make sure you add

        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);
        }
    

    Then just use 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">
    
    0 讨论(0)
提交回复
热议问题