How to open .html files in my application?

-3

I'm doing an application for Android and wanted to know how to open and display a specific .html file located on the SD card directly in my application.

    
asked by anonymous 08.06.2016 / 16:14

1 answer

3

It will depend a lot on where the file is, being a specific location, first add a webView to your app, in this case the name is webView1 , if you use another ID for your webView you should change the examples below the webView1 by the ID you are using.

Then you need the Environment class:

The code should look something like:

WebView meuWebView = (WebView) findViewById(R.id.webView1);

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    Log.d("Teste html no SD", "sem cartão SD");
} else {
    String path = Environment.getExternalStorageDirectory();

    //Esta linha é apenas para teste
    Log.d("Teste html no SD", "file://" + path + "/pasta/arquivo.html");

    meuWebView.loadUrl("file://" + path + "/pasta/arquivo.html");
}

I do not have the ADT installed so I could not test, but if the above example does not work and the path "file://" + path + "/pasta/arquivo.html" is wrong (as Log.d ), then try this:

WebView meuWebView = (WebView) findViewById(R.id.webView1);

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    Log.d("Teste html no SD", "sem cartão SD");
} else {
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();

    //Esta linha é apenas para teste
    Log.d("Teste html no SD", "file://" + path + "/pasta/arquivo.html");

    meuWebView.loadUrl("file://" + path + "/pasta/arquivo.html");
}
    
08.06.2016 / 16:47