gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:27:55 28 4
gpt4 key购买 nike

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

关于android - Webview 在加载本地 HTML 文件时显示空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32319080/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com