Network image loading in Android4.0

Source: Internet
Author: User

<P> 4.0 the system cannot load network images in the main thread. Otherwise, an exception is reported. You can use the new thread method to solve the problem: </p>


Package com. smalt. thread;

Import java. io. InputStream;

Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. OS. AsyncTask;
Import android. util. Log;
Import android. widget. ImageView;

/**
* Display network images in system 4.0
*
* @ Author Administrator
*
*/
Public class DownLoadImage extends AsyncTask <String, Void, Bitmap> {
ImageView imageView;

Public DownLoadImage (ImageView imageView ){
// TODO Auto-generated constructor stub
This. imageView = imageView;
}

@ Override
Protected Bitmap doInBackground (String... urls ){
// TODO Auto-generated method stub
String url = urls [0];
Bitmap tmpBitmap = null;
Try {
InputStream is = new java.net. URL (url). openStream ();
TmpBitmap = BitmapFactory. decodeStream (is );
} Catch (Exception e ){
E. printStackTrace ();
Log. I ("test", e. getMessage ());
}
Return tmpBitmap;
}

@ Override
Protected void onPostExecute (Bitmap result ){
// TODO Auto-generated method stub
ImageView. setImageBitmap (result );
}
}

Image loading method:

String uriBaidu = "https://www.baidu.com/img/baidu_logo.gif ";

New DownLoadImage (ImageView) findviewbyid(r.id.iv3).exe cute (uriBaidu );

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.