Android网络通信之网络图片查看器

转载

一个简单的小应用——网络图片查看器。

根据应用需求,先创建一个web应用,向其中放入一张图片之后部署在Tomcat上。

通过EditText显示网络图片路径,Button控制图片显示,ImageView显示图片。

界面部分代码:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".ImageLookerActivity" >      
  8.     <!-- 输入框 -->  
  9.     <EditText  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/imagePath"  
  13.         android:text="http://172.16.59.35:8080/lss/images/blackboard.jpg"       
  14.     />  
  15.     <!-- 按钮 -->  
  16.     <Button  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:id="@+id/button"  
  20.         android:text="@string/button"     
  21.     />  
  22.     <!-- 显示图片组件 -->  
  23.     <ImageView  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:id="@+id/imageView"      
  27.     />    
  28. </LinearLayout>  

界面效果:

Activity部分代码:

 

[java]  view plain copy
  1. public class MainActivity extends Activity {  
  2.     EditText text;  
  3.     ImageView image;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);        
  8.         text=(EditText)this.findViewById(R.id.imagePath);  
  9.         image=(ImageView)this.findViewById(R.id.imageView);  
  10.         Button button=(Button)this.findViewById(R.id.button);  
  11.         button.setOnClickListener(new ButtonClickListener());  
  12.     }        
  13.   //定义一个按钮监听事件内部类  
  14. private final class ButtonClickListener implements OnClickListener  
  15.     {  
  16.         public void onClick(View v) {  
  17.             //点击按钮,得到文本框中的图片路径  
  18.             String path=text.getText().toString();  
  19.             byte[] data;  
  20.             try {  
  21.                 //定义一个ImageService业务类,以字节数组的形式得到图片数据  
  22.                 data = ImageService.getImage(path);  
  23.                 //使用数组数据生成位图的对象  
  24.                 Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);  
  25.                 //将位图放入图片控件  
  26.                 image.setImageBitmap(bitmap);  
  27.             } catch (IOException e) {  
  28.                 // TODO Auto-generated catch block  
  29.                 e.printStackTrace();  
  30.                 Toast.makeText(getApplicationContext(), R.string.error, 1).show();  
  31.             }             
  32.         }         
  33.     }  
  34. }  

ImageService业务类

 

[java]  view plain copy
  1. public class ImageService {  
  2.     /** 
  3.      * 获取网络图片的数据 
  4.      */  
  5.     public static byte[] getImage(String path) throws IOException {  
  6.         URL url=new URL(path);  
  7.         //得到一个基于http协议的链接对象  
  8.         HttpURLConnection conn=(HttpURLConnection)url.openConnection();  
  9.         //设置链接的超时时间  
  10.         conn.setConnectTimeout(5000);  
  11.         conn.setRequestMethod("GET");  
  12.        //取得服务器返回的相应码  
  13.         if(conn.getResponseCode()==200)  
  14.         {    InputStream inputStream=conn.getInputStream();   
  15.              //定义一个流工具类,将输入流中的内容以字节形式返回  
  16.              return StreamTool.read(inputStream);          
  17.         }          
  18.         return null;  
  19.     }  
  20. }  

StreamTool工具类

 

[java]  view plain copy
  1. public class StreamTool {  
  2. /** 
  3.  * 读取流中的数据 
  4.  */  
  5.     public static byte[] read(InputStream inputStream) throws IOException {  
  6.         byte[] buffer=new byte[1024];  
  7.         ByteArrayOutputStream outStream=new ByteArrayOutputStream();  
  8.         int len=0;  
  9.         while((len=inputStream.read(buffer))!=-1)  
  10.         {  
  11.             outStream.write(buffer, 0, buffer.length);  
  12.         }  
  13.         //关闭输入流  
  14.         inputStream.close();  
  15.         //返回内存中数据  
  16.         return outStream.toByteArray();  
  17.     }  
  18. }  

注意申请网络访问权限:

[html]  view plain copy
  1. <!-- 允许网络访问 -->   
  2. lt;uses-permission android:name="android.permission.INTERNET"/>  

步骤总结:

1、首先得到输入流

InputStream inputStream=conn.getInputStream();

2、根据资源类型转换成对应数据。

Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值