2016-11-29 122 views
4

語境:加載圖像

我的應用程序有時會顯示「表」,這是包含文本和圖片的HTML文件:

<p>image description </p> 
<p><img src="/api/medias/get/videos/56afad6447eba61c8099544b?thumbnail=true&amp;company=4f86d0a9d655ab9794f5d1d2&amp;fullSizedCover=true" 
    alt="" 
    data-id="56afad6447eba61c8099544b" 
    data-type="video" data-width="640" data-height="1136" /></p> 

然後我用body.loadDataWithBaseURL(<my api url>, body, "text/html; charset=UTF-8", null, null);

我不認爲它是相關的,但我會說,以防萬一,我有一個模板正文,其中包含CSS和JavaScript。該js腳本檢測圖像點擊並將「data-id」傳輸給android方法(通過JavascriptInterface)。在這種情況下,它會打開一個視頻播放器並播放它。


問題:

我的應用程序允許用戶下載這些表的後面離線vizualisation。所以,我下載HTML,然後下載圖片到我的本地私有目錄(Context.getFileDir()),並更改HTML設置「縮略圖」來源我下載的圖像的src:

<p>video</p> 
<p><img src="69c623955ecb5bd414f908cd383f3809.jpg" 
    alt="" 
    data-id="56afad6447eba61c8099544b" 
    data-type="video" data-width="640" data-height="360" /></p> 

我的問題是:什麼我把基地網址放到我的webview中以獲得預期的行爲(即:顯示下載的圖像)。

我試過Context.getFileDir().getAbsolutePath(),content://com.android.htmlfileprovider和其他一些。

我應該改變嗎?

感謝了很多,


這非常適用:

Picasso.with(iv.getContext()).load(new File(getContext().getFilesDir() + "/" + "69c623955ecb5bd414f908cd383f3809.jpg")).into(iv);

的HTML:

<p>video</p> 
<p><img src="69c623955ecb5bd414f908cd383f3809.jpg" alt="" data-id="56afad6447eba61c8099544b" data-type="video" data-width="640" data-height="360" /></p> 

我基地網址:baseUrl = Uri.fromFile(getContext().getFilesDir()).toString();

並最終: webview.loadDataWithBaseURL(baseUrl, body, "text/html; charset=UTF-8", null, null);

+0

「我嘗試了Context.getFileDir()。getAbsolutePath(),」 - 嘗試使用'file'方案。更容易使用'Uri.fromFile(getFilesDir())。toString()'。 – CommonsWare

+0

我的圖片仍然不會顯示這個baseUrl:'file:/// data/user/0//files',因爲'new File(Context.getFilesDir()+「/」+「69c623955ecb5bd414f908cd383f3809.jpg」) .exists()'==真。 –

+0

@CommonsWare也是有用的嗎? (以前沒有,只是添加它們,但似乎沒有改變任何東西): - 'body.getSettings()。setAllowFileAccess(true);' - 'body.getSettings()。setAllowContentAccess(true) ;' - 'body.getSettings()。setAllowFileAccessFromFileURLs(true);' - 'body.getSettings()。setAllowUniversalAccessFromFileURLs(true);' –

回答

0

如果您已經下載了/有模板,您可以嘗試使用null baseUrl,並將該模板作爲body傳遞給webview。 並通過之前查找img標籤,並設置img src與本地存儲的文件的完整路徑。如下所示:

Document doc = Jsoup.parse(newHtml); 
Elements elems = doc.getElementsByTag("img"); 
for (Element el : elems) { 
    String filename = el.attr("src"); //presumably only name 
    String picUri = "file:///" + folder + "/"+filename; 
    el.attr("src", picUri); 
} 
web.loadDataWithBaseURL(null, htmlBody, "text/html", "UTF-8", null); 
+0

我不知道爲什麼,但確實,它不會工作把「基地」的基礎網址,但傳遞null,並將其手動內部src標籤工程 –