To display images on the Web in Android, you need to first find the image address according to the URL, then convert the image to Java InputStream, then convert the InputStream stream into bitmap, Bitmap can be displayed directly in the ImageView of Android. This is the idea of displaying the network film, the implementation is very simple. Let's take a look at the implementation process.
First, in Androidmanifest.xml, add access to the Internet to the program:
<uses-permissionandroid:name= "Android.permission.INTERNET"/>
Then add a imageview to the layout file to display the pictures on the network:
1 <?XML version= "1.0" encoding= "Utf-8"?> 2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android" 3 Android:layout_width= "Fill_parent" 4 Android:layout_height= "Fill_parent" 5 android:orientation= "vertical" > 6 7 <TextView8 Android:layout_width= "Fill_parent" 9 Android:layout_height= "Wrap_content" Ten Android:text= "@string/hello" /> One <ImageView A Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:id= "@+id/imageview" /> the - </LinearLayout>
in the main program activity to write from the network to get pictures, and converted into InputStream, and then into the ImageView can be displayed in the bitmap.
1 PackageCom.image;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 Importjava.net.HttpURLConnection;6 Importjava.net.MalformedURLException;7 ImportJava.net.URL;8 9 Importandroid.app.Activity;Ten ImportAndroid.graphics.Bitmap; One Importandroid.graphics.BitmapFactory; A ImportAndroid.os.Bundle; - ImportAndroid.widget.ImageView; - the Public classNetimageactivityextendsActivity { - /**Called when the activity is first created.*/ -String imageUrl = "Http://content.52pk.com/files/100623/2230_102437_1_lit.jpg"; - Bitmap bmimg; + ImageView Imview; - @Override + Public voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.main); -Imview =(ImageView) Findviewbyid (R.id.imageview); - Imview.setimagebitmap (Returnbitmap (IMAGEURL)); - } - - PublicBitmap returnbitmap (String url) { inURL Myfileurl =NULL; -Bitmap Bitmap =NULL; to Try { +Myfileurl =Newurl (URL); -}Catch(malformedurlexception e) { the E.printstacktrace (); * } $ Try { Panax NotoginsengHttpURLConnection conn =(httpurlconnection) Myfileurl - . OpenConnection (); theConn.setdoinput (true); + Conn.connect (); AInputStream is =Conn.getinputstream (); theBitmap =Bitmapfactory.decodestream (IS); + Is.close (); -}Catch(IOException e) { $ E.printstacktrace (); $ } - returnbitmap; - } the}
Then run the program to display the pictures on the network.
View network Pictures