2012-01-20 36 views
0

我想獲得文本作爲跨越,以獲得文本的html視圖。 但是,我不想要images.Is那可能嗎?如何從imageGetter獲得空白圖像

這是我的代碼:

 Spanned description2=Html.fromHtml(des.get(position),null,null); 
    TextView text = (TextView) dialog.findViewById(R.id.descr); 
      text.setText(description2); 

但它返回我一些綠色的框,這一點我不想要的照片請幫忙

帶有圖像

字符串內容描述2 = des.get (位置)的ToString();

Spanned description = Html.fromHtml(description2, new ImageGetter() { 
      @Override 
     public Drawable getDrawable(String source) {     
      Drawable d = null; 
      try { 
         InputStream src = imageFetch(source); 
         d = Drawable.createFromStream(src, "src"); 
         if(d != null){ 
     d.setBounds(0,0,d.getIntrinsicWidth(), 
     d.getIntrinsicHeight()); 

         } 
     } catch (MalformedURLException e) { 
        e.printStackTrace(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 

     return d; 
     } 

      public InputStream imageFetch(String source) 
        throws MalformedURLException,IOException { 
     URL url = new URL(source); 
     Object o = url.getContent(); 
     InputStream content = (InputStream)o; 
     // add delay here (see comment at the end)  
     return content; 
     } 

     },null); 

回答

1

您必須重寫ImageGettergetDrawable實施方式,因爲默認實現返回那些醜陋的綠色方塊:

private class ImageGetter implements Html.ImageGetter 
{ 
    @Override 
    public Drawable getDrawable(String source) 
    { 
     return new BitmapDrawable(); 
    } 
}; 

,然後切換當前的代碼:

ImageGetter imageGetter = new ImageGetter(); 
Spanned description2=Html.fromHtml(des.get(position),imageGetter,null); 
    TextView text = (TextView) dialog.findViewById(R.id.descr); 
      text.setText(description2); 

現在,關於您在評論中提出的問題,您必須卸載UI線程的實際下載,請嘗試使用this post(ta看看我的答案,這是使用線程與處理程序的方式),如果仍不清楚,請再次詢問。

+0

感謝隊友,這有效!但是,如果我想獲取圖像,有一個很大的延遲,直到圖像加載..有沒有辦法解決它?一般來說,是一個好主意,使用spanned獲取圖像和來自rss的文字? – user1156210

+0

即時通訊問,因爲用這種方式,綠色方塊消失(正如我們想要的),但仍然存在的空間.. – user1156210

+0

如果你想獲得圖像,那麼你必須重寫ImageGetter不同,你可以開始一個線程將使用處理程序或AsyncTask下載所有圖像並在屏幕上顯示它們。 – Rotemmiz