加载图片的工具类 package com.android1.news.common; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.lang.ref.SoftReference; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.support.v4.util.LruCache; public class LoadImage { //private static Map<String, SoftReference<Bitmap>> softReferences=new HashMap<String, SoftReference<Bitmap>>(); private static LruCache<String, Bitmap> cache=new LruCache<String, Bitmap>(1024*1024*3); private Context context; private ImageLoadListener listener; public LoadImage(Context context,ImageLoadListener listener){ this.context=context; this.listener=listener; } public interface ImageLoadListener{ void imageLoadOk(Bitmap bitmap,String url); } public Bitmap geBitmap(String url){ Bitmap bitmap=null; if(url==null || url.length()<=0) return bitmap; //1.先看看内存中有没有 bitmap=getBitmapFromReference(url); if(bitmap!=null){ System.out.println("1.内存中的图片"); return bitmap; } //2.去看看缓存文件有没有 bitmap=getBitmapFromCache(url); if(bitmap!=null){ //存入内存 //softReferences.put(url, new SoftReference<Bitmap>(bitmap)); cache.put(url, bitmap); System.out.println("2.缓存中的图片"); return bitmap; } //3.异步加载 getBitmapAsync(url); return bitmap; } private void getBitmapAsync(String url) { ImageAsyncTask imageAsyncTask=new ImageAsyncTask(); imageAsyncTask.execute(url); } private Bitmap getBitmapFromCache(String url) { //http://www.baidu.com/gejge/jgejg/gg.jpg String name=url.substring(url.lastIndexOf("/")+1); //File cacheDir=context.getCacheDir(); File cacheDir=context.getExternalCacheDir(); File[] files=cacheDir.listFiles(); if(files==null){ return null; } File bitmapFile=null; for (File file : files) { if(file.getName().equals(name)){ bitmapFile=file; break; } } if(bitmapFile==null){ return null; } Bitmap bitmap=BitmapFactory.decodeFile(bitmapFile.getAbsolutePath()); if(bitmap==null) return null; return bitmap; } private Bitmap getBitmapFromReference(String url) { Bitmap bitmap=null; // if(softReferences.containsKey(url)){ // bitmap=softReferences.get(url).get(); // } bitmap=cache.get(url); return bitmap; } private class ImageAsyncTask extends AsyncTask<String, Void, Bitmap>{ private String url; @Override protected Bitmap doInBackground(String... params) { url=params[0]; //params[0]--->url Bitmap bitmap=null; try { URL url=new URL(params[0]); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); InputStream is=conn.getInputStream(); bitmap=BitmapFactory.decodeStream(is); // //软引用 // softReferences.put(params[0], new SoftReference<Bitmap>(bitmap)); // System.out.println(softReferences.size()); cache.put(params[0], bitmap); //缓存文件 saveCacheFile(params[0],bitmap); System.out.println("3.网络中的图片"); } catch (Exception e) { e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if(listener!=null){ listener.imageLoadOk(result,url); } } } public void saveCacheFile(String url,Bitmap bitmap) { String name=url.substring(url.lastIndexOf("/")+1); //File cacheDir=context.getCacheDir(); File cacheDir=context.getExternalCacheDir(); if(!cacheDir.exists()){ cacheDir.mkdirs(); } OutputStream stream; try { stream = new FileOutputStream(new File(cacheDir, name)); bitmap.compress(CompressFormat.JPEG, 100, stream); } catch (FileNotFoundException e) { e.printStackTrace(); } } } 适配器 package com.android1.news.ui.adapter; import com.android1.news.R; import com.android1.news.common.CommonUtil; import com.android1.news.common.LoadImage; import com.android1.news.model.entity.News; import com.android1.news.ui.base.MyBaseAdapter; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class NewsAdapter extends MyBaseAdapter<news> { private Bitmap defaultBitmap; private LoadImage loadImage; private ListView listView; public NewsAdapter(Context context,ListView listView) { super(context); defaultBitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.defaultpic); loadImage=new LoadImage(context, listener); this.listView=listView; } @Override public View getMyView(int position, View convertView, ViewGroup parent) { HoldView holdView=null; if(convertView==null){ convertView=inflater.inflate(R.layout.item_list_news, null); holdView=new HoldView(convertView); convertView.setTag(holdView); }else{ holdView=(HoldView) convertView.getTag(); } News news=myList.get(position); holdView.tv_title.setText(news.getTitle()); holdView.tv_text.setText(news.getSummary()); holdView.tv_from.setText(news.getAuthor()); holdView.iv_list_image.setImageBitmap(defaultBitmap);//默认图片 String url=news.getList_image(); holdView.iv_list_image.setTag(CommonUtil.APPURL+url); System.out.println(CommonUtil.APPURL+url); Bitmap bitmap=loadImage.geBitmap(CommonUtil.APPURL+url); if(bitmap!=null){ holdView.iv_list_image.setImageBitmap(bitmap); } return convertView; } public class HoldView { public ImageView iv_list_image; public TextView tv_title; public TextView tv_text; public TextView tv_from; public HoldView(View view) { iv_list_image = (ImageView) view.findViewById(R.id.imageView1); tv_title = (TextView) view.findViewById(R.id.textView1); tv_text = (TextView) view.findViewById(R.id.textView2); tv_from = (TextView) view.findViewById(R.id.textView3); } } private LoadImage.ImageLoadListener listener=new LoadImage.ImageLoadListener(){ @Override public void imageLoadOk(Bitmap bitmap, String url) { ImageView iv=(ImageView) listView.findViewWithTag(url); if(iv!=null) iv.setImageBitmap(bitmap); } }; } </news>