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

时间:2015-08-31 20:04:26

标签: android html android-webview

我正在编写一个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>

我已经尝试了许多针对此问题的在线解决方案,包括:

到目前为止没有任何作用......

1 个答案:

答案 0 :(得分:1)

因此,如果要从外部存储加载它们,则必须添加权限:

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

试试这个网址:

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