Android gets images from the network and sets the cache

Source: Internet
Author: User
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 show11         ImageView image = (ImageView) findViewById(R.id.image);12         13         // Image url14         String image_url = "http://10.0.2.2/biyeshejidata/www.png";15         16         // ImageLoader class instance17         ImageLoader imgLoader = new ImageLoader(getApplicationContext());18         19         // whenever you want to load an image from url20         // call DisplayImage function21         // url - image url to load22         // loader - loader image, will be displayed before getting image23         // 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         else11             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 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.