频道栏目
首页 > 资讯 > Android基础教程 > 正文

第101章、读取网络图片(从零开始学Android)

16-02-02        来源:[db:作者]  
收藏   我要投稿
Android手机上,我们常用ImageView显示图片,我们本章获取网络图片并显示在ImageView中。

一、设计界面

1、布局文件

打开res/layout/activity_main.xml文件。
输入以下代码:

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3. android:orientation="vertical"   
  4. android:layout_width="match_parent"   
  5. android:layout_height="match_parent" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imagephoto"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12. </LinearLayout>   

 

二、程序文件

打开“src/com.genwoxue.networkphoto/MainActivity.java”文件。
然后输入以下代码:

[java] view plain copy
 
  1. package com.genwoxue.networkphoto;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.MalformedURLException;  
  7. import java.net.URL;  
  8. import android.app.Activity;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.os.AsyncTask;  
  12. import android.os.Bundle;  
  13. import android.widget.ImageView;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     private ImageView imView;   
  18.     @Override   
  19.     public void onCreate(Bundle savedInstanceState) {    
  20.         super.onCreate(savedInstanceState);    
  21.         setContentView(R.layout.activity_main);    
  22.           
  23.         imView = (ImageView) findViewById(R.id.imagephoto);    
  24.         String imageUrl = "../../img/image/ilogob.gif";    
  25.           
  26.         new NetworkPhoto().execute(imageUrl);  
  27.     }  
  28.   
  29.     /* 四个步骤: 
  30.      * (1)onPreExecute(),执行预处理,它运行于UI线程, 
  31.      * 可以为后台任务做一些准备工作,比如绘制一个进度条控件。 
  32.      * (2)doInBackground(Params...),后台进程执行的具体计算在这里实现, 
  33.      * doInBackground(Params...)是AsyncTask的关键,此方法必须重载。 
  34.      * 在这个方法内可以使用 publishProgress(Progress...)改变当前的进度值。 
  35.      * (3)onProgressUpdate(Progress...),运行于UI线程。如果 
  36.      * 在doInBackground(Params...) 中使用了publishProgress(Progress...),就会 
  37.      * 触发这个方法。在这里可以对进度条控件根据进度值做出具体的响应。 
  38.      * (4)onPostExecute(Result),运行于UI线程,可以对后台任务的结果做出处理,结果 
  39.      * 就是doInBackground(Params...)的返回值。此方法也要经常重载,如果Result为 
  40.      * null表明后台任务没有完成(被取消或者出现异常)。    *  
  41.      */  
  42.       
  43.     //本案例我们仅使用了(2)和(4)  
  44.     class NetworkPhoto extends AsyncTask<String, Integer, Bitmap> {   
  45.         public NetworkPhoto() {  
  46.         }  
  47.       
  48.         //doInBackground(Params...),后台进程执行的具体计算在这里实现,是AsyncTask的关键,此方法必须重载。  
  49.         @Override   
  50.         protected Bitmap doInBackground(String... urls) {    
  51.             URL url = null;    
  52.             Bitmap bitmap = null;    
  53.             HttpURLConnection conn=null;    
  54.             InputStream is=null;    
  55.           
  56.             try {     
  57.                 url = new URL(urls[0]);    
  58.             } catch (MalformedURLException e) {    
  59.                 e.printStackTrace();    
  60.             }  
  61.                   
  62.             try {     
  63.                 conn = (HttpURLConnection) url.openConnection();     
  64.                 conn.setDoInput(true);     
  65.                 conn.connect();     
  66.                 is = conn.getInputStream();     
  67.                 bitmap = BitmapFactory.decodeStream(is);     
  68.                 is.close();    
  69.             } catch (IOException e) {     
  70.                 e.printStackTrace();    
  71.             } finally  {     
  72.                 if(conn!=null){      
  73.                     conn.disconnect();      
  74.                     conn=null;     
  75.                 }     
  76.               
  77.                 if(is!=null)   {      
  78.                     try {  
  79.                         is.close();  
  80.                     } catch (IOException e) {  
  81.                       e.printStackTrace();  
  82.                     }      
  83.                     is=null;   
  84.                 }  
  85.             }  
  86.         return bitmap;  
  87.     }  
  88.   
  89.         //onPostExecute(Result),运行于UI线程,可以对后台任务的结果做出处理,结果  
  90.         //就是doInBackground(Params...)的返回值。  
  91.         @Override       
  92.         protected void onPostExecute(Bitmap bitmap) {      
  93.             // 返回结果bitmap显示在ImageView控件            
  94.             imView.setImageBitmap(bitmap);        
  95.         }    
  96.     }  
  97.   
  98. }  


三、配置文件

打开“AndroidManifest.xml”文件。

然后输入以下代码:

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.genwoxue.networkphoto"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="10"  
  9.         android:targetSdkVersion="14" />  
  10.   
  11.     <uses-permission android:name="android.permission.INTERNET" />  
  12.       
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.genwoxue.networkphoto.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  

 

注意:需要在AndroidManifest.xml文件中添加权限:

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

四、运行结果

相关TAG标签
上一篇:第105章、蓝牙(从零开始学Android)
下一篇:Win7系统右键菜单添加删除选项的设置方法
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站