Augusto
Augusto

Reputation: 115

Webview displaying blank pages when loading local HTML files

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...

Upvotes: 1

Views: 2264

Answers (2)

Leon Sky
Leon Sky

Reputation: 11

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

Upvotes: 1

Lucas Crawford
Lucas Crawford

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

Related Questions