我将以下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
中的解决方案。我还确保图像没有损坏(我可以使用Gallery应用程序查看这些图像)。有什么想法吗?
我在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>