View network Pictures

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.