Android从网络获取图片并设置缓存

public class AndroidLoadImageFromURLActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Loader image - will be shown before loading image
        int loader = R.drawable.loader;
        
        // Imageview to show
        ImageView image = (ImageView) findViewById(R.id.image);
        
        // Image url
        String image_url = "http://10.0.2.2/biyeshejidata/www.png";
        
        // ImageLoader class instance
        ImageLoader imgLoader = new ImageLoader(getApplicationContext());
        
        // whenever you want to load an image from url
        // call DisplayImage function
        // url - image url to load
        // loader - loader image, will be displayed before getting image
        // image - ImageView 
        imgLoader.DisplayImage(image_url, loader, image);
    }
}
View Code
 1 public class AndroidLoadImageFromURLActivity extends Activity {
 2     @Override
 3     public void onCreate(Bundle savedInstanceState) {
 4         super.onCreate(savedInstanceState);
 5         setContentView(R.layout.main);
 6         
 7         // Loader image - will be shown before loading image
 8         int loader = R.drawable.loader;
 9         
10         // Imageview to show
11         ImageView image = (ImageView) findViewById(R.id.image);
12         
13         // Image url
14         String image_url = "http://10.0.2.2/biyeshejidata/www.png";
15         
16         // ImageLoader class instance
17         ImageLoader imgLoader = new ImageLoader(getApplicationContext());
18         
19         // whenever you want to load an image from url
20         // call DisplayImage function
21         // url - image url to load
22         // loader - loader image, will be displayed before getting image
23         // image - ImageView 
24         imgLoader.DisplayImage(image_url, loader, image);
25     }
26 }
View Code
 1 public class FileCache {
 2     
 3     private File cacheDir;
 4  
 5     public FileCache(Context context){
 6         //Find the dir to save cached images
 7         
 8         if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
 9             cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"data");
10         else
11             cacheDir=context.getCacheDir();
12         if(!cacheDir.exists())
13             cacheDir.mkdirs();
14     }
15  
16     public File getFile(String url){
17         String filename=String.valueOf(url.hashCode());
18         File f = new File(cacheDir, filename);
19         return f;
20  
21     }
22  
23     public void clear(){
24         File[] files=cacheDir.listFiles();
25         if(files==null)
26             return;
27         for(File f:files)
28             f.delete();
29     }
30  
31 }
View Code
 1 public class MemoryCache {
 2     private Map<String, SoftReference<Bitmap>> cache=Collections.synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());
 3  
 4     public Bitmap get(String id){
 5         if(!cache.containsKey(id))
 6             return null;
 7         SoftReference<Bitmap> ref=cache.get(id);
 8         return ref.get();
 9     }
10  
11     public void put(String id, Bitmap bitmap){
12         cache.put(id, new SoftReference<Bitmap>(bitmap));
13     }
14  
15     public void clear() {
16         cache.clear();
17     }
18 }
View Code
 1 public class Utils {
 2     public static void CopyStream(InputStream is, OutputStream os)
 3     {
 4         final int buffer_size=1024;
 5         try
 6         {
 7             byte[] bytes=new byte[buffer_size];
 8             for(;;)
 9             {
10               int count=is.read(bytes, 0, buffer_size);
11               if(count==-1)
12                   break;
13               os.write(bytes, 0, count);
14             }
15         }
16         catch(Exception ex){}
17     }
18 }
原文地址:https://www.cnblogs.com/LIANQQ/p/2846038.html