Android reads network images through URLs

Source: Internet
Author: User

Note: 1. After android4.0, the Code Cannot uninstall the ui.

2. Think about it. Just get the network image through the url. I just gave it a url and then it gave it to me. Here is a simple implementation method:

Private class DownLoadImage extends AsyncTask
 
  
{ImageSwitcher imageSwitcher; public DownLoadImage (ImageSwitcher is) {this. imageSwitcher = is;} protected Bitmap doInBackground (String... urls) {System. out. println ("loading images asynchronously starts! "); String url = urls [0]; //" http://ww3.sinaimg.cn/bmiddle/6e91531djw1e8l3c7wo7xj20f00qo755.jpg "; System. out. println (url); Bitmap tmpBitmap = null; try {InputStream is = new java.net. URL (url ). openStream (); tmpBitmap = BitmapFactory. decodeStream (is); is. close ();} catch (Exception e) {e. printStackTrace (); Log. I ("KK download image", e. getMessage ();} return tmpBitmap;} @ Overrideprotected void onProgressUpdate (Integer... values) {// TODO Auto-generated method stubsuper. onProgressUpdate (values); System. out. println ("process progress:" + values);} protected void onPostExecute (Bitmap result) {// TODO: // convert bitmap to drawable Resources res = getResources (); drawable bd = new BitmapDrawable (res, result); imageSwitcher. setImageDrawable (bd); System. out. println ("Asynchronous image loading is complete! ");}}
 

Call new downloadimage(switchercmd.exe cute (img_url); In the ui thread. The ImageSwitcher imageSwitcher in the function can be changed to imageview or another control.

These two lines are also used to read images using the above method.

 InputStream is = new java.net.URL(url).openStream();      tmpBitmap = BitmapFactory.decodeStream(is);

It is very concise, but the problem comes: I want to make a progress bar to display the progress of image download. This inputstream seems to be overly encapsulated.

This reminds me of the Yin and Yang figure. It may be necessary to implement the progress bar. I will go back and use the method with a lot of code!


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.