2013-01-17 31 views
2

我有HTML字符串如下图像不是TextView中使用Html.fromhtml

String htmlstr=" This is my image <img src='/sdcard/pic1.jpg' /> and the my second image is <img src='/sdcard/pic2.jpg' />" 

我使用

txtimg.setText(Html.fromHtml(htmlstr)); 

但问题是它会显示1个默认小squre用绿色显示代替图片

请帮我展示图片

在此先感谢

+0

退房此链接 [1]:http://stackoverflow.com/questions/7161280/image-in-html-of-textview -not-being-displayed-properly – GingerJim

+0

@GinerJim:谢谢你的评论,但我想显示来自SD卡的图像,我试过imageGetter,但它不适合我的工作你有任何想法如何使用imageGetter在我的情况下 – Hemantwagh07

+0

@Hemantxp:它不适合你的情况呢? –

回答

5

试试这个:

final String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.jpg"; 

ImageGetter imageGetter = new ImageGetter() { 
    public Drawable getDrawable(String source) { 
     Drawable d = Drawable.createFromPath(path); 
     d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
     return d; 
    } 
}; 

Spanned htmlstr= Html.fromHtml("<img src='" + path + "'/>", imageGetter, null); 
TextView out_unit1 = (TextView) findViewById(R.id.mTxtViewCm2); 
out_unit1.setText(htmlstr); 

它很适合我。

谢谢。

+0

感谢您的回答但我修改了ImageGetter的一些逻辑,它为我工作 我会把我的代码作为答案 – Hemantwagh07

3

它为我工作如下

txtimg.setText(Html.fromHtml(htmlstr, new ImageGetter() {     
       @Override 
       public Drawable getDrawable(String source) { 
        String path = source; 

        Drawable bmp = Drawable.createFromPath(path); 
        bmp.setBounds(0, 0, bmp.getIntrinsicWidth(), bmp.getIntrinsicHeight()); 

        return bmp; 
       } 
      }, null)); 
相关问题