Android從網路擷取圖片並設定緩衝

來源:互聯網
上載者: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 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.