簡體   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