How to read and load the internal storage .csv file in webview

Viewed 79

I am working in xamarin android project. I am downloading .CSV file and store it in internal storage. File path is given below. filepath: /storage/emulated/0/Android/data/com.abc.def/files/ABC/MyListSample.csv

I want to load this .csv file into webview. I tried the below given code but couldn't able to load the .csv file in webview.

_webview = (WebView)FindViewById(Resource.Id.webView);

_webview.SetInitialScale (0);
        _webview.Settings.JavaScriptEnabled = true;
        _webview.Settings.SetSupportMultipleWindows (false);
        _webview.Settings.AllowFileAccess = true;
        _webview.Settings.SetPluginState (WebSettings.PluginState.On);//Try
        _webview.Settings.BuiltInZoomControls = false;
        _webview.Settings.JavaScriptCanOpenWindowsAutomatically = true;

_webview.Settings.AllowUniversalAccessFromFileURLs = true;

_webview.LoadUrl(string.Format("file://" + filepath));

Second method I tried is given below:

 Java.IO.File csv = new Java.IO.File(filepath);

 _webview.LoadUrl(string.Format("file://" + csv));

I don't want any third party library like open csv to view/load this csv file. I have searched a lot and tried many methods but couldn't able to find solution.

1 Answers

I have done a sample to test your code. I can open the .html file and the image file with it, but I got a blank webview when I try to load the .csv file.

It seems the webview doesn't support to open the .csv file directly. But I used the following code to open a .csv file online by the google documents view.

 _webview.LoadUrl("https://docs.google.com/viewer?embedded=true&url=" + "https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv");

But this code can't load the internal storage .csv file. So you can try to upload the .csv to a server.

Related