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 ! :)
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.
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.
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");
}
I think the url is file:///sdcard/FMS/1/message.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With