当前位置:操作系统 > 安卓/Android >>

Android TextView 显示HTML加图片

TextView显示网络图片,我用android2.3的系统,可以显示图片出来,并且如果图片比较大,应用会卡的现象,肯定是因为使用主线程去获取网络图片造成的,但如果我用android4.0以上的系统运行,则不能显示图片,只显示小方框。


究其原因,是在4.0的系统上执行的时候报错了,异常是:android.os.NetworkOnMainThreadException 经过查文档,原来是4.0系统不允许主线程(UI线程)访问网络,因此导致了其异常。说白了就是在主线程易做图问网络,会造成主线程挂起,系统不允许使用了。

 


此处有作部分修改,代码独立。图片实现异步加载。解决上述问题

用法,调用代码activity

 

[html]
//TextView 控件 
textViewContent = (TextView) getActivity().findViewById(R.id.textview_prodcut_detail_more_zp_content); 
//HTML文本 
zp_content = "测试图片信息:<br><img src=\"http://b2c.zeeeda.com/upload/2013/05/10/136814766742544.jpg\" />"; 
//默认图片,无图片或没加载完显示此图片 
Drawable defaultDrawable = MainActivity.ma.getResources().getDrawable(R.drawable.stub); 
//调用 
Spanned sp = Html.fromHtml(zp_content, new HtmlImageGetter(textViewContent, "/esun_msg", defaultDrawable), null); 
textViewContent.setText(sp); 

//TextView 控件
textViewContent = (TextView) getActivity().findViewById(R.id.textview_prodcut_detail_more_zp_content);
//HTML文本
zp_content = "测试图片信息:<br><img src=\"http://b2c.zeeeda.com/upload/2013/05/10/136814766742544.jpg\" />";
//默认图片,无图片或没加载完显示此图片
Drawable defaultDrawable = MainActivity.ma.getResources().getDrawable(R.drawable.stub);
//调用
Spanned sp = Html.fromHtml(zp_content, new HtmlImageGetter(textViewContent, "/esun_msg", defaultDrawable), null);
textViewContent.setText(sp);
HtmlImageGetter类[html]
mport java.io.InputStream; 
 
import android.graphics.Canvas; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.os.AsyncTask; 
import android.os.Environment; 
import android.text.Html.ImageGetter; 
import android.util.Log; 
import android.widget.TextView; 
 
public class HtmlImageGetter implements ImageGetter{ 
    private TextView _htmlText; 
    private String _imgPath; 
    private Drawable _defaultDrawable; 
    private String TAG = "HtmlImageGetter"; 
  
    public HtmlImageGetter(TextView htmlText, String imgPath, Drawable defaultDrawable){ 
        _htmlText = htmlText; 
        _imgPath = imgPath; 
        _defaultDrawable = defaultDrawable; 
    } 
  
    @Override 
    public Drawable getDrawable(String imgUrl) { 
  
        String imgKey = String.valueOf(imgUrl.hashCode()); 
        String path = Environment.getExternalStorageDirectory() + _imgPath; 
        FileUtil.createPath(path); 
  
        String[] ss = imgUrl.split("\\."); 
        String imgX = ss[ss.length-1]; 
        imgKey = path+"/" + imgKey+"."+imgX; 
  
  
        if(FileUtil.exists(imgKey)){ 
            Drawable drawable = FileUtil.getImageDrawable(imgKey); 
            if(drawable != null){ 
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
                return drawable; 
            }else{ 
                Log.v(TAG,"load img:"+imgKey+":null"); 
            } 
        } 
  
        URLDrawable urlDrawable = new URLDrawable(_defaultDrawable); 
        new AsyncThread(urlDrawable).execute(imgKey, imgUrl); 
        return urlDrawable; 
    } 
  
    private class AsyncThread extends AsyncTask<String, Integer, Drawable> { 
        private String imgKey; 
        private URLDrawable _drawable; 
  
        public AsyncThread(URLDrawable drawable){ 
            _drawable = drawable; 
        } 
  
        @Override 
        protected Drawable doInBackground(String... strings) { 
            imgKey = strings[0]; 
            InputStream inps = NetWork.getInputStream(strings[1]); 
            if(inps == null) return _drawable; 
  
            FileUtil.saveFile(imgKey, inps); 
            Drawable drawable = Drawable.createFromPath(imgKey); 
            return drawable; 
        } 
  
        public void onProgressUpdate(Integer... value) { 
  
        } 
  
        @Override 
        protected void onPostExecute(Drawable result) { 
            _drawable.setDrawable(result); 
            _htmlText.setText(_htmlText.getText()); 
        } 
    } 
  
    public class URLDrawable extends BitmapDrawable { 
  
        private Drawable drawable; 
  
        public URLDrawable(Drawable defaultDraw){ 
            setDrawable(defaultDraw); 
        } 
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,