1

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:

Nothing works so far...

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Augusto
  • 115
  • 1
  • 10
  • is your assets folder in the root directory of your project, or in src/main/assets/index.html – Lucas Crawford Aug 31 '15 at 20:11
  • My assets folder is in the root directory of my project. The problem is not with HTML pages coming from the assets folder, those display correctly. The problem is that I now moved my HTML pages to the device's storage, and these just display a blank page. – Augusto Aug 31 '15 at 20:19
  • My mistake on an early answer, I did not read the question all the way through :) I get it now, let me think for a second on it – Lucas Crawford Aug 31 '15 at 20:21
  • try my answer below @augusto – Lucas Crawford Aug 31 '15 at 20:30

2 Answers2

1

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";
Lucas Crawford
  • 3,078
  • 2
  • 14
  • 25
1

do u use Samsung phone? if yes go app staore uninstall "android system webview" after that will working fine.

Leon Sky
  • 11
  • 1