Reputation: 115
I am coding an Android app that correctly loads and displays local HTML content from assets folder. These are very simple pages, with just pictures and text (no buttons nor javascript).
Because of some last minute requirements, these HTML files now need to be loaded from the device's file system. I've moved the files around and updated their paths, but now my app just displays blank pages (no errors).
My original code (loading from the assets folder):
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
String pagePath = "file:///android_asset/content/cover.html";
LayoutInflater inflater =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.webview, null);
WebView webView = (WebView) view.findViewById(R.id.webView);
// The app performs better with these
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.loadUrl(pagePath);
return view;
}
In my current code - where I try to display the same pages from the device's storage - I've just changed the pagePath:
String pagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/content/cover.html";
My layout (webview.xml) is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></WebView>
</LinearLayout>
I've tried many of the solutions found online for this problem, including:
Changing the layout to RelativeLayout (https://stackoverflow.com/a/30599912/2430853)
Adding a myWebView.setWebViewClient(new WebViewClient()); (Android WebView showing blank page for local files)
Using android:hardwareAccelerated="false" (https://stackoverflow.com/a/22801655/2430853)
Or simply setting a series of options such as setDomStorageEnabled(true); (https://stackoverflow.com/a/31040469/2430853); or, setUseWideViewPort(true); and setLoadWithOverviewMode(true) (https://stackoverflow.com/a/14301637/2430853)
Nothing works so far...
Upvotes: 1
Views: 2264
Reputation: 11
do u use Samsung phone? if yes go app staore uninstall "android system webview" after that will working fine.
Upvotes: 1
Reputation: 3118
So if you are loading them from external storage, you must add permissions to do so:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And try this for the url:
String url = "file:///" + Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "content/myFile.html";
Upvotes: 1