Android: imageview how to display network images
Address: http://hi.baidu.com/programmar/blog/item/79483ecb2ac75cf552664fd3.html
Displaying a network image in Android is actually super simple. The following is a very simple example:
Step 1:
1. Create Your activity, which is described in viewwebimageactivity in this example;
2. The code in viewwebimageactivity is as follows:
String imageurl = "http://hiphotos.baidu.com/baidu/pic/item/7d8aebfebf3f9e125c6008d8.jpg"; // This is the network picture you need to display --- online casually find
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;
}
3. The returnbitmap (string URL) method is used to convert a network image to a bitmap.
Step 2:
1. Modify your main. xml file 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:
1. In your androidmanifest. add <uses-Permission Android: Name = "android. permission. internet "/>. This is because android has a lot of permission restrictions. Otherwise, Images cannot be displayed on your simulator.
In this way, your goal has been achieved. It's easy, so don't try it now!