Open HTML file from assets folder

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

     
    

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

提交回复
热议问题