Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webview cannot load html file from sd card

I'm trying to load a html file from sd-card. Note: -> if i load http://www.google.com it works. -> the file exists -> i have permissions for internet and WRITE_EXTERNAL_STORAGE

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    addB = (Button) findViewById(R.id.add);
    webComp = (WebView) findViewById(R.id.webC);

    WebSettings webSettings = webComp.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(false);
    webSettings.setAllowFileAccess(true);
    webSettings.setLoadsImagesAutomatically(true);
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);        

    webComp.setWebViewClient(new HelloWebViewClient());                    



    webComp.loadUrl("/sdcard/FMS/1/message.html");            



}

Thank you ! :)

like image 639
Misca Avatar asked Mar 29 '11 13:03

Misca


People also ask

How do I load HTML into WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

How do I view HTML files on Android?

Step 1: To add a local HTML file into your Android project there must be an asset folder in it. To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.


2 Answers

Misca,

You shouldn't hard code the directory of the sdcard like that. Its typically at /mnt/sdcard/ but this is never assured. You should also always check if the sdcard exists and is mounted first!

You want to use the following:

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    Log.d(TAG, "No SDCARD");
} else {
    webComp.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/FMS/1/message.html");
}
like image 132
Will Tate Avatar answered Oct 20 '22 03:10

Will Tate


I think the url is file:///sdcard/FMS/1/message.html

like image 37
Jett Hsieh Avatar answered Oct 20 '22 01:10

Jett Hsieh