3

I currently created an app for android.
In this app I successfully display an XLS file, thanks to the google doc viewer and passing the file the URL like this:

WebView mWebView=new WebView(MyActivity.this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo);
    setContentView(mWebView);

So, now I need to display the same XLS file which I saved into my device storage, not from an URL this time, but directly from the storage.

I found lots of advices to make it with a link to the file in the URL, but not with my file already onto my device.

So, before I start, I'd like to know if it is possible?

I saw this method myWebView.loadData( );

Can I use it for my problem?
Can I use docs.google.com/gview with an internal file?

EDIT

I try the solution of Der Gol...lum

This is my code :

WebView wv = (WebView)findViewById(R.id.fileWebView);
    wv.getSettings().setJavaScriptEnabled(true);
    String root = Environment.getExternalStorageDirectory().toString();
    String my_path = root + "/excel_files/";
    wv.loadUrl("file://" + my_path + excelFile.getName());
    wv.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    });

But the webview display a blank page

Slasch
  • 275
  • 1
  • 6
  • 20
  • 1
    @DerGol...lum Thank ! I'll try ! Yes it's only for display file no modification is possible, so there are no problem for static and immuatable file =) – Slasch Jul 30 '15 at 09:23

1 Answers1

1

Assuming that your file is static and immutable, you can put the file in your assets folder, and use

mWebView.loadUrl("file:///android_asset/your_file");

to load into your WebView.
I use this technique for displaying help (HTML files).

You may also use subfolders, under assets (in my case, I use an URL such as: "file:///android_asset/help/help_" + language + ".htm"),
because my file is in assets/help/.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I think i have a problem, because my file is get from a webservice. I saved in my external storage and after I displayed it. So i can put my file in assets folder – Slasch Jul 31 '15 at 13:12
  • 1
    Then read it from the storage... something like `"file://" + your_path + "your_file"`. your_path should include the "/" path separator before the file name. – Phantômaxx Jul 31 '15 at 13:16
  • Ok something like this ? `String root = Environment.getExternalStorageDirectory().toString(); wv.loadUrl("file://" + root + "/excel_files/" + excelFile.getName());` – Slasch Jul 31 '15 at 13:39
  • Yes, something so. by your_path I really meant something like `String your_path = Environment.getExternalStorageDirectory().toString() + "excel_files/"` – Phantômaxx Jul 31 '15 at 13:53
  • I have nothing in my webview see my code in my edit. Maybe it's because the extention of my file is **.xls** and i need a **.htm** no ? – Slasch Jul 31 '15 at 14:14
  • Aren't these words **true** `In this app I successfully display an XLS file, thanks to the google doc viewer and passing the file the URL like this:`? It seems that your WebView is able to display **xls** files. Or I didn't get you? – Phantômaxx Jul 31 '15 at 14:26
  • No it's working with this call `wv.loadUrl("http://docs.google.com/gview?url=" + "http://lecompagnon.info/demos/demoxl3.xls");` but this is a url from web page and i want to display the file from my device storage – Slasch Jul 31 '15 at 14:30
  • Uh... so, I think it's an online feature only. You might **try** (without warranties) to pass `wv.loadUrl("docs.google.com/gview?url=" + your_local_path + ")"`, but I **seriously** doubt it will work. You might want to try a different approach, though: http://stackoverflow.com/questions/7170180/viewing-excel-files-in-my-android-app – Phantômaxx Jul 31 '15 at 14:33
  • 1
    Thank for your help Der Gol...lum and sorry for the missunderstanding. I 'll try some other possibilities – Slasch Jul 31 '15 at 14:45
  • 1
    There are the Apache POI extensions. http://www.mysamplecode.com/2011/10/android-read-write-excel-file-using.html – Phantômaxx Jul 31 '15 at 14:49