2016-03-14 27 views
1

在Android開發中,如何從下面顯示的HTML文本中獲取圖片URL或加載圖片?我把它從HTML,我想從這個代碼圖片網址:如何從html文本中獲取img url

<p style="text-align: justify;"><span style="font-size: 16px;"><img class="aligncenter wp-image-2699 size-large" src="http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-1024x258.jpg" alt="helal parti-1" width="750" height="189" srcset="http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-300x76.jpg 300w, http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-768x193.jpg 768w, http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-1024x258.jpg 1024w, http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-560x141.jpg 560w, http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1.jpg 1564w" sizes="(max-width: 750px) 100vw, 750px" /></span></p>

我想SRC =「http://www.helalsaglikliyasam.org/wp-內容/上傳/ 2016/02 /希拉勒-雜色1-1024x258.jpg」

回答

1

您應該使用HTML解析器 - 可以通過快速搜索找到幾個Java HTML解析庫。

一個快速和骯髒,方式,但是,這是搜索的輸入字符串爲src="聲明,就像這樣:

int index = input.indexOf("src=\""); 
String substr = input.substring(index + 5); 
int endIndex = substr.indexOf("\""); 
String imgUrl = substr.substring(0, endIndex); 

免責聲明:我沒有測試過這一點,所以它可能有錯誤。它也會做出很多假設,這可能不是真的 - 這就是爲什麼你應該使用圖書館這種事情!

編輯:修復了測試後的一個錯誤(必須使用與我輸入的不同的機器)。它現在應該爲你工作 - 但是,你應該真的使用一個庫。

0

創建以HTML只是類型的圖像:

<img src="www.example.com/images/this-one.png" alt="an image" />

您可以根據需要將img標記嵌套到p和/或span標記中。 只需要注意,使用外部樣式表時代碼更容易閱讀

+0

對不起,我的意思是在Android開發。我從html代碼中獲得它,並且想從此代碼中獲取圖像網址 –