Open HTML file from assets folder

后端 未结 5 1690
没有蜡笔的小新
没有蜡笔的小新 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: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!

提交回复
热议问题