简体   繁体   中英

How to load Html file from assets folder into WebView

I have tried a couple of things to get this issue worked out including examples i found on the web but no to avail. It displays absolutely nothing. Below are my codes. Please help me i don't know where i get it wrong

And i have internet permission in my AndroidMainfest.xml: uses-permission android:name="android.permission.INTERNET"

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/tv01" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>




public class LocalDialogActivity extends Activity {

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

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

        webView = (WebView)findViewById(R.id.local_tv01);
        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(URL);
    }


All thanks to @CommonsWare for providing me with this wonderful solution I made this answer for others who have this type of problem. It is working now. This is exactly how i put it to work


    protected WebView webView;
    private static final String URL = "file:///android_asset/dancerkate.html";

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

        webView = (WebView)findViewById(R.id.local_tv01);
        webView.setWebViewClient(new WebViewClient());
        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);
        }



        refreshWebView(webView);

    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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