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://www.bkjia.com/uploadfile/2012/0317/20120317094731393.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.
Excerpt from advancing with the times