且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【android基础】之Android获取网络上的图片结合ImageView的简单应用

更新时间:2021-09-25 01:11:40

网络的访问在我们日常生活中太重要了,如果没有网络我们的生活将会是什么样子呢?android手机和浏览器也是一样的,也可以通过网络通讯获取数据,如调用webservice,EJB等。下面就通过一个小例子从网络获取一幅图片并显示在手机上,开发中将会使用到一个新的组件ImageView.

1.写一个用来处理字节流的工具类


package org.lxh.util;  
  1.   
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.InputStream;  
  4.   
  5. public class StreamTool {  
  6.     public static byte[] readInputStream(InputStream in) throws Exception{  
  7.         int len=0;  
  8.         byte buf[]=new byte[1024];  
  9.         ByteArrayOutputStream out=new ByteArrayOutputStream();  
  10.         while((len=in.read(buf))!=-1){  
  11.             out.write(buf,0,len);  //把数据写入内存   
  12.         }  
  13.         out.close();  //关闭内存输出流   
  14.         return out.toByteArray(); //把内存输出流转换成byte数组   
  15.     }  
  16.   
  17. }  

2.写一个得到图片byte数组的service类


package org.lxh.service;  
  1.   
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10.   
  11. import org.lxh.util.StreamTool;  
  12.   
  13. import android.util.Log;  
  14.   
  15. public class WebService {  
  16.     public static byte[] getImage(String path){  
  17.           
  18.         URL url;  
  19.         byte[] b=null;  
  20.         try {  
  21.             url = new URL(path);   //设置URL   
  22.             HttpURLConnection con;  
  23.               
  24.             con = (HttpURLConnection)url.openConnection();  //打开连接   
  25.           
  26.             con.setRequestMethod("GET"); //设置请求方法   
  27.             //设置连接超时时间为5s   
  28.             con.setConnectTimeout(5000);  
  29.             InputStream in=con.getInputStream();  //取得字节输入流   
  30.           
  31.             b=StreamTool.readInputStream(in);  
  32.               
  33.         } catch (Exception e) {  
  34.               
  35.             e.printStackTrace();  
  36.         }  
  37.         return b;  //返回byte数组   
  38.           
  39.     }  
  40.       
  41. }  

3.写一个用户操作界面


<?xml version="1.0" encoding="utf-8"?>  
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6. <TextView    
  7.     android:layout_width="fill_parent"   
  8.     android:layout_height="wrap_content"   
  9.     android:text="@string/hello"  
  10.     />  
  11.     <TextView    
  12.     android:layout_width="fill_parent"   
  13.     android:layout_height="wrap_content"   
  14.     android:text="@string/picaddress"  
  15.     />  
  16.     <EditText  
  17.     android:layout_width="wrap_content"   
  18.     android:layout_height="wrap_content"   
  19.     android:text="http://www.desk9.com/Desk9Image/21/Desk9_21_1690_35790_S.jpg"  
  20.     android:id="@+id/imageaddress"  
  21.     />  
  22.     <Button  
  23.      android:layout_width="wrap_content"   
  24.     android:layout_height="wrap_content"   
  25.     android:text="@string/look"  
  26.     android:id="@+id/button"  
  27.     />  
  28.     <ImageView  
  29.      android:layout_width="fill_parent"   
  30.     android:layout_height="fill_parent"   
  31.     android:id="@+id/image"/>  
  32. </LinearLayout>  


4.写一个activity类

package org.lxh.net;  

  1.   
  2. import org.lxh.service.WebService;  
  3.   
  4. import android.app.Activity;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.ImageView;  
  13. import android.widget.Toast;  
  14.   
  15. public class NetActivity extends Activity {  
  16.    private EditText picaddress;  
  17.    private Button button;  
  18.    private ImageView imageView;  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         button=(Button)this.findViewById(R.id.button);  
  23.         imageView=(ImageView)this.findViewById(R.id.image);  
  24.         picaddress=(EditText)this.findViewById(R.id.imageaddress);  
  25.         button.setOnClickListener(new View.OnClickListener() {  
  26.               
  27.             public void onClick(View v) {  
  28.                 String address=picaddress.getText().toString();  
  29.                   
  30.                 try {  
  31.                   
  32.                     byte[] data=WebService.getImage(address); //得到图片的输入流   
  33.                       
  34.                     //二进制数据生成位图   
  35.                     Bitmap bit=BitmapFactory.decodeByteArray(data, 0, data.length);  
  36.                       
  37.                     imageView.setImageBitmap(bit);  
  38.                   
  39.                 } catch (Exception e) {  
  40.                     Log.e("NetActivity", e.toString());  
  41.                       
  42.                     Toast.makeText(NetActivity.this, R.string.error, 1).show();  
  43.                 }  
  44.             }  
  45.         });  
  46.     }  
  47. }  

5.添加网络访问的权限

<?xml version="1.0" encoding="utf-8"?>  
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.       package="org.lxh.net"  
  3.       android:versionCode="1"  
  4.       android:versionName="1.0">  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <activity android:name=".NetActivity"  
  7.                   android:label="@string/app_name">  
  8.             <intent-filter>  
  9.                 <action android:name="android.intent.action.MAIN" />  
  10.                 <category android:name="android.intent.category.LAUNCHER" />  
  11.             </intent-filter>  
  12.         </activity>  
  13.   
  14.     </application>  
  15.     <uses-sdk android:minSdkVersion="7" />  
  16.   
  17.     <uses-permission android:name="android.permission.INTERNET"/>  
  18. </manifest>   

6.这里把strings.xml文件也贴出来


<?xml version="1.0" encoding="utf-8"?>  

  1. <resources>  
  2.     <string name="hello">Hello World, NetActivity!</string>  
  3.     <string name="app_name">图片查看</string>  
  4.     <string name="picaddress">图片地址</string>  
  5.     <string name="look">查看</string>  
  6.     <string name="error">网络连接异常</string>  
  7. </resources>  

下面是运行效果图,代码我也上传给大家。

【android基础】之Android获取网络上的图片结合ImageView的简单应用