Android View Network Picture example

Source: Internet
Author: User

1. Layout files


<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:orientation=" vertical "Android:layout_heig ht= "Match_parent" tools:context= ". Mainactivity "> <imageview android:id=" @+id/iv_icon "android:layout_width=" Match_parent "and roid:layout_height= "Wrap_content" android:layout_weight= "1"/> <linearlayout android:layout_wid Th= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > & Lt EditText android:id= "@+id/et_url" android:layout_height= "Wrap_content" Android:layout_wi Dth= "0dip" android:text= "Http://img0.bdstatic.com/img/image/shouye/dengni37.jpg" android:singleline= "True" android:layout_weight= "1"/> <button android:id= "@+id/btn_submit" and Roid:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:text= "Go"/> </Li Nearlayout></linearlayout>

2. Implementation in the main program

Package Com.wzw.netphoto;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Android.os.bundle;import Android.app.Activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.view.menu;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Activity implements Onclicklistener {private ImageView ivicon;private EditText eturl; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); IvIcon = ( ImageView) Findviewbyid (r.id.iv_icon); Eturl = (EditText) Findviewbyid (R.id.et_url); Button btn= (button) Findviewbyid (r.id.btn_submit); Btn.setonclicklistener (this);} @Overridepublic void OnClick (View v) {String url=eturl.gettext (). toString (); Bitmap BIT=GETIMAGEFRomnet (URL); ivicon.setimagebitmap (bit);} Public Bitmap getimagefromnet (String url) {try {url murl=new url (URL);//Create URL object HttpURLConnection conn = (  httpurlconnection) murl.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (10000); Set the connection timeout time conn.setreadtimeout (5000);//Set Read timeout conn.connect (); int responsecode = Conn.getresponsecode (); if ( responsecode==200) {InputStream is = Conn.getinputstream (); Bitmap Bitmap=bitmapfactory.decodestream (IS); Toast.maketext (This, "Get Success", 0). Show (); return bitmap;}} catch (Exception e) {e.printstacktrace ();} return null;}}

3. Add Network access rights

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.wzw.netphoto "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>", <!--Add permissions to access the network and <uses-permiss Ion Android:name= "Android.permission.INTERNET"/> <application android:allowbackup= "true" Android:ic on= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <            Activity android:name= "com.wzw.netphoto.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <cate Gory android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> & Lt;/application></manifest>


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.