Andriod read the network picture Instance code parsing _android

Source: Internet
Author: User

On Android phones, we use ImageView to display pictures, and we get network pictures in this chapter and display them in ImageView.

First, design interface

1. layout file

Open the Res/layout/activity_main.xml file.

Enter the following code:

<?xml version= "." Encoding= "utf-"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/res/ Android " 
android:orientation=" vertical " 
android:layout_width=" match_parent " 
android:layout_height= "Match_parent" > 
<imageview 
android:id= "@+id/imagephoto" 
android:layout_width= "Wrap_content" 
android:layout_height= "Wrap_content"/> 

Ii. Procedural documents

Open the "Src/com.genwoxue.networkphoto/mainactivity.java" file.

Then enter the following code:

Package Com.genwoxue.networkphoto; 
Import java.io.IOException; 
Import Java.io.InputStream; 
Import java.net.HttpURLConnection; 
Import java.net.MalformedURLException; 
Import Java.net.URL; 
Import android.app.Activity; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import Android.os.AsyncTask; 
Import Android.os.Bundle; 
Import Android.widget.ImageView; 
public class Mainactivity extends activity {private ImageView Imview; 
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_main); 
Imview = (ImageView) Findviewbyid (R.id.imagephoto); 
String imageUrl = "Yun_qi_img/ilogob.gif"; 
New Networkphoto (). Execute (IMAGEURL); 
/* Four steps: * () OnPreExecute (), performs preprocessing, runs on the UI thread, * can do some preparation work for background tasks, such as drawing a progress bar control. * () Doinbackground (Params ...), the specific computations performed by the background process are implemented here, * Doinbackground (Params ...) 
is the key to Asynctask, this method must be overloaded. * You can use Publishprogress (Progress ...) in this method. 
Changes the current progress value. * () onprogressupdate (Progress ...)), running on the UI thread. If you use Publishprogress (Progress ...) in Doinbackground (Params ...), you will trigger this method. 
Here you can make a specific response to the progress bar control based on the progress value. * () OnPostExecute (result), run on the UI thread, can deal with the results of the background task, the results are Doinbackground (Params ...) 's return value. This method is also frequently overloaded if result is * null indicating that the background task did not complete (was canceled or an exception occurred).  
* *///This case we only use () and () class Networkphoto extends Asynctask<string, Integer, bitmap> {public Networkphoto () {} 
Doinbackground (Params ...), the implementation of the specific calculation of the background process is implemented here, is the key to Asynctask, this method must be overloaded. 
@Override protected Bitmap doinbackground (String ... urls) {URL url = null; 
Bitmap Bitmap = null; 
HttpURLConnection Conn=null; 
InputStream Is=null; 
try {url = new URL (urls[]); 
catch (Malformedurlexception e) {e.printstacktrace (); 
try {conn = (httpurlconnection) url.openconnection (); 
Conn.setdoinput (TRUE); 
Conn.connect (); 
is = Conn.getinputstream (); 
Bitmap = Bitmapfactory.decodestream (IS); 
Is.close (); 
catch (IOException e) {e.printstacktrace (); 
finally {if (conn!=null) {conn.disconnect (); 
Conn=null; } if (Is!=null) {try {is.close (); 
catch (IOException e) {e.printstacktrace (); 
} is=null; 
} return bitmap; //onpostexecute (Result), run on the UI thread, can deal with the results of the background task, and/or Doinbackground (Params ...) 
's return value. 
@Override protected void OnPostExecute (Bitmap Bitmap) {//return result Bitmap displayed in ImageView control Imview.setimagebitmap (BITMAP); } 
} 
}

Third, the configuration file

Open the "androidmanifest.xml" file.

Then enter the following code:

<?xml version= "." Encoding= "utf-"?> <manifest xmlns:android= 
"http://schemas.android.com/apk/res/ Android " 
package=" Com.genwoxue.networkphoto " 
android:versioncode=" " 
android:versionname=". "> 
<uses-sdk 
android:minsdkversion= "" 
android:targetsdkversion= ""/> 
<uses-permission Android:name= "Android.permission.INTERNET"/> 
<application 
android:allowbackup= "true" 
android:icon= "@drawable/ic_launcher" 
android:label= "@string/app_name" 
android:theme= "@style/apptheme "> 
<activity 
android:name=" com.genwoxue.networkphoto.MainActivity " 
android:label=" @string /app_name "> 
<intent-filter> 
<action android:name=" Android.intent.action.MAIN "/> 
<category android:name= "Android.intent.category.LAUNCHER"/> 
</intent-filter> 
</ Activity> 
</application> 

Note: You need to add permissions in the Androidmanifest.xml file:

 <uses-permission android:name= "Android.permission.INTERNET"/>

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.