2015-05-22 87 views
1

我想要顯示存儲在具有html標記(,< im /> ...)的字符串中的圖像(png,gif)。 顯示Html沒有問題,但圖像不起作用。 我在互聯網上看到的例子,但只有一個字符串,只包含一個圖像沒有文本和更多。TextView with Html + <img base64/>

description.setText(Html.fromHtml(new.getDescription())); 

例子:

"<p>Text before image</p>\r\n<p><img style="max-width: 100%;" src="data:image/gif;base64,R0lGODlhMgA2APQAACgoKDg4ODiQaFCQEHi4IEDYcGiIuIhYKLhoIMAYIPh/></p>"

THX所有答案。

回答

0

總之,要做到這一點,你需要使用Html.fromHtml的擴展超載:

description.setText(Html.fromHtml(new.getDescription(), new CustomImageGetter(), null)); 

而且你必須創建CustomImageGetter本身(它實現ImageGetter),使CustomImageGetter.getDrawable回報一個由你的字節串構造的drawable。做一個WebView或者將字節字符串保存爲一個資源並使用CustomImageGetter來獲取它可能更容易。

這個問題類似於另一個堆棧溢出問題:android-TextView setText in Html.fromHtml to display image and text,它可以幫助您設置一個CustomImageGetter,它將從資源返回一個drawable。然後你就可以開始玩弄字節串了。