首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 前辈们帮小弟我看看这个异步加载图片错哪了

2013-06-25 
android 前辈们帮我看看这个异步加载图片哪里错了?//绑定数据MyImageAdpatermyImageAdpater new MyImageA

android 前辈们帮我看看这个异步加载图片哪里错了?
//绑定数据
MyImageAdpater  myImageAdpater =new MyImageAdpater (list);
listView.setAdapter(myImageAdpater);
//自定义的MyImageAdpater  如下:
  protected class MyImageAdpater extends BaseAdapter{
    private List<Company> list;
public MyImageAdpater(List<Company> result) {
this.list=result;
}

@Override
public int getCount() {
return (list==null?0:list.size());
}

@Override
public Object getItem(int position) {
return  (list==null? null : list.get(position));
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
 LayoutInflater  layoutInflater= LayoutInflater.from(getApplicationContext());
 View view=layoutInflater.inflate(R.layout.company_show_listview, null);
  //图片显示 需要异步加载图片
             ImageView imageView=(ImageView) view.findViewById(   
           R.id.CompanyShowImageViewID);
ImgeAsyncLoading imgeAsyncLoading=new ImgeAsyncLoading(); imgeAsyncLoading.loadingImg(imageUrl,imageView);
     return  view;
}
  
  }

ImgeAsyncLoading 异步加载图片的类:如下:
   public class ImgeAsyncLoading {
private Handler handler;
private HashMap<String, SoftReference<Bitmap>> imageCache= new HashMap<String, SoftReference<Bitmap>>();

    /**
     * 异步加载图片方法
     * @param imagePath 图片路径
     * @param imageView ImageView控件
     */
public void loadingImg(final String imagePath,final ImageView imageView) {
// 检查SoftReference软引用是否存在对象
if (imageCache.containsKey(imagePath)) {
System.out.println("----检查软引用");
SoftReference<Bitmap> softReference = imageCache.get(imagePath);
Bitmap bitmap = softReference.get();
if (bitmap != null) {// 未被回收直接设置图片到控件上
 imageView.setImageBitmap(bitmap);
 return ;

}

      //软引用为空 就去网络下载
handler = new Handler() {
//得到下载好的图片   
@Override
public void handleMessage(Message msg) {
Bitmap bitmap=(Bitmap) msg.obj;
    //放入到软引用中
imageCache.put(imagePath, new  SoftReference<Bitmap>(bitmap));
//更新ui界面
 imageView.setImageBitmap(bitmap);
}
};
//开启线程下载图片
ImageRunable imageRunable = new ImageRunable(imagePath);
Thread thread = new Thread(imageRunable);
thread.start();
}

private class ImageRunable implements Runnable {
private String url;

public ImageRunable(String path) {
this.url = path;
}

@Override
public void run() {
HttpClient client = null;


try {
HttpGet httpGet = new HttpGet(url);
client = new DefaultHttpClient();
// 请求超时
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
// 读取超时
client.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 60000 * 3);
HttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Message message = handler.obtainMessage();
message.obj = bitmap;
handler.sendMessage(message);
inputStream.close();
}
} catch (Exception e) {
throw new AskHttpException(e);
} finally {
// 关闭连接
if (client != null) {
client.getConnectionManager().shutdown();
}

}
}
}
}

这个有没错误啊,我怎么和网上不一样,网上用了回调函数,回调函数我搞不懂,就没用回调,不知道这样做对不对哦,图片是没有错误,但是我们测试的时候,为什么连System.out.println("----检查软引用");
这句话都没有输出来啊。是没有起作用吗?程序哪里错了吗?

    
   Android 图片 Bitmap imageview
[解决办法]
贴出log看下,程序貌似没什么问题。
[解决办法]
public MyImageAdpater(List<Company> result) {
this.list=result;
notifyDataSetChanged();//加一句这个试试;
}

[解决办法]
解决了没??????话说,有问题,可以自己先解决的,因为这不是很大的问题
[解决办法]
当然不会显示出来了.你这个软引用就没起作用.

在getView里你获取View的时候,每次都新建一个ImgeAsyncLoading对象,在该对象里创建一个软引用,这样每次都会将图片放在新的软引用里,所以该log打不出来.

可以把软引用放到ImgeAsyncLoading类之上一层,Adapter里定义,或者设置成static变量,就OK了.

热点排行