Webview 在加载本地 HTML 文件时显示空白页

Webview displaying blank pages when loading local HTML files

我正在编写一个 Android 应用程序,它可以正确加载和显示 assets 文件夹中的本地 HTML 内容。这些页面非常简单,只有图片和文字(没有按钮,也没有 javascript)。

由于一些临时要求,现在需要从设备的文件系统加载这些 HTML 文件。我移动了文件并更新了它们的路径,但现在我的应用只显示空白页(没有错误)。

我的原始代码(从 assets 文件夹加载):

    @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;
    }

在我当前的代码中 - 我尝试显示设备存储中的相同页面 - 我刚刚更改了 pagePath:

String pagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/content/cover.html";

我的布局(webview.xml)如下:

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

对于这个问题,我已经尝试了很多在网上找到的解决方案,包括:

到目前为止没有任何效果...

因此,如果您要从外部存储加载它们,则必须添加权限才能这样做:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

并为 url 尝试此操作:

String url = "file:///" + Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "content/myFile.html";

你用三星吗phone? 如果是,去应用商店卸载 "android system webview" 之后就可以正常工作了。