This example describes the Android display network image method, share for everyone to reference. The specific methods are as follows:
In general, displaying a Web image in Android is very simple, and here's a very simple example:
Step 1:
① Create your activity, in this case the viewwebimageactivity description;
The code in ②viewwebimageactivity is as follows:
Copy Code code as follows:
String imageUrl = "Yun_qi_img/logo.gif"; This is the web image you need to display---the Internet.
Bitmap bmimg;
ImageView Imview;
Button button1;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Imview = (ImageView) Findviewbyid (R.id.imview);
Imview.setimagebitmap (Returnbitmap (IMAGEURL));
}
Public Bitmap returnbitmap (String URL) {
URL myfileurl = null;
Bitmap Bitmap = null;
try {
myfileurl = new URL (URL);
catch (Malformedurlexception e) {
E.printstacktrace ();
}
try {
HttpURLConnection conn = (httpurlconnection) myfileurl.openconnection ();
Conn.setdoinput (TRUE);
Conn.connect ();
InputStream is = Conn.getinputstream ();
Bitmap = Bitmapfactory.decodestream (IS);
Is.close ();
catch (IOException e) {
E.printstacktrace ();
}
return bitmap;
}
③ among them, the Returnbitmap (String URL) method is to implement the network image conversion into bitmap.
Step 2:
Modify your Main.xml file as follows:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<imageview
Android:id= "@+id/imview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"/>
</linearlayout>
Step 3:
Add it to the node of your Androidmanifest.xml file, because Android has a lot of restrictions on the permissions, otherwise the images can't be displayed on your emulator.
I hope this article will help you with your Android program.