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.