繁体   English   中英

来自 SD 卡的图像未加载到 WebView

[英]Images from SD Card is not loaded in the WebView

我将以下 HTML 代码加载到String中。

<html>
    <body>
        <img style="width:100%" src="file://storage/emulated/0/sdcard/Download/Page1.jpg"/>
        <img style="width:100%" src="file://storage/emulated/0/sdcard/Download/Page2.jpg"/>
        <img style="width:100%" src="file://storage/emulated/0/sdcard/Download/Page3.jpg"/>
        <img style="width:100%" src="file://storage/emulated/0/sdcard/Download/Page4.jpg"/>
    </body>
</html>

我正在尝试将 html 加载到我的应用程序中的WebView中,如下所示。

webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", "");

我在AndroidManifest.xml中具有以下权限,并确保已授予权限。

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

我在 SO 中经历了很多答案(例如this ),但似乎找不到将 SD 卡中的图像加载到我的WebView的解决方案。 我还确保图像没有损坏(我可以使用图库应用查看这些图像)。 有什么想法吗?

我在 logcat 中没有发现任何错误。 但是,与此问题相关的可能 logcat 可能如下(但我不确定)。

W/cr_CrashFileManager: /data/user/0/com.example.myapp/cache/WebView/Crash Reports does not exist or is not a directory

这是WebView尝试加载图像时的样子。

在此处输入图像描述

在此先感谢您的时间!

而不是使用Environment.getExternalStorageDirectory我使用/sdcard/Downloads这似乎解决了这个问题。 所以我生成的有效的最终 HTML 如下。

<html>
    <body>
        <img style="width:100%" src="file://sdcard/Download/Page1.jpg"/>
        <img style="width:100%" src="file://sdcard/Download/Page2.jpg"/>
        <img style="width:100%" src="file://sdcard/Download/Page3.jpg"/>
        <img style="width:100%" src="file://sdcard/Download/Page4.jpg"/>
    </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM