The ImageView method of using network picture resources in Android _android

Source: Internet
Author: User
Tags time limit

This article is an example of how ImageView uses network image resources in Android. Share to everyone for your reference. Specifically as follows:

Most of the time we don't want to put everything in the apk, or we can't put it in, we need a universal network to help us achieve it.

The screenshot of the running effect is as follows:

The Java code is as follows:

Package Com.android.antking.imageview;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.ImageView;
 public class Mainactivity extends activity {//define a picture display control private ImageView ImageView; /** called the activity is a.
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
  Picture resource String url = "http://s16.sinaimg.cn/orignal/89429f6dhb99b4903ebcf&690";
  Get the picture available Bitmap Bitmap = gethttpbitmap (URL);
  ImageView = (ImageView) This.findviewbyid (R.ID.IMAGEVIEWID);
 Display Imageview.setimagebitmap (bitmap); /** * Get Web Image resource * @param URL * @return/public static Bitmap gethttpbitmap (String url) {URL MyfIleurl;
  Bitmap Bitmap=null;
   try{myfileurl = new URL (URL);
   Get Connected HttpURLConnection conn= (httpurlconnection) myfileurl.openconnection ();
   Set timeout time to 6000 milliseconds, Conn.setconnectiontiem (0), indicating no time limit conn.setconnecttimeout (6000);
   The connection setting obtains the data stream Conn.setdoinput (true);
   Do not use cache conn.setusecaches (false);
   This sentence is dispensable, does not affect//conn.connect ();
   Gets the data stream InputStream is = Conn.getinputstream ();
   Parse get picture bitmap = Bitmapfactory.decodestream (IS);
  Close Data Flow is.close ();
  }catch (Exception e) {e.printstacktrace ();
 return bitmap;

 }
}

I hope this article will help you with your Android program.

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.