Android development-network image acquisition

Source: Internet
Author: User

Android development-network image acquisition

Used for completing the settings! Obtain network images

Here is a brief introduction to my situation.

 

First, design the layout. Drag a few widgets and then click OK.

Start adding features in Acitivity

Take a picture of the link online: http://www.chzu.edu.cn/page/main1152/images/banner.jpg

Paste the main code:

 

private Bitmap getUrlImageMap() {URL imageUrl = null;Bitmap bitmap = null;try {imageUrl = new URL(url);HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();conn.connect();InputStream is = conn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();} catch (Exception e) {e.printStackTrace();}return bitmap;}
The above is to get the bitmap value of the image through the HttpUrlConnection link, and then write a class that inherits the AsyncTask to load the image.

 

Pay attention to doInbackground () and onPostExecute (). The former processes time-consuming operations and the latter processes UI updates. Do not make a mistake in this regard.

 

class loadImage extends AsyncTask
 
  {Bitmap aBitmap;@Overrideprotected Void doInBackground(Void... params) {aBitmap = getUrlImageMap();return null;}@Overrideprotected void onPostExecute(Void result) {super.onPostExecute(result);imageView.setImageBitmap(aBitmap);}}
 
Run, the interface is as follows

 

The example is very simple. Pay attention to the acquisition of one piece and asynchronous loading and UI updates. Click here to download the DEMO.

 

 

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.