android从网络中获得一张图片,并显示在屏幕上

1: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="cn.capinftotech.image"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".MainActivity"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.     </application>  
  16.     <uses-sdk android:minSdkVersion="8" />  
  17.     <uses-permission android:name="android.permission.INTERNET" />  
  18.   
  19. </manifest>   
注意访问网络中的数据需要添加android.permission.INTERNET权限

2:MainActivity的内容

[java]  view plain copy
  1. package cn.capinftotech.image;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.ImageView;  
  14. import android.widget.Toast;  
  15.   
  16. import com.capinfotech.service.ImageService;  
  17.   
  18. public class MainActivity extends Activity {  
  19.     private static final String TAG = "MainActivity";  
  20.       
  21.     private EditText urlPath = null;  
  22.     private Button button = null;  
  23.     private ImageView imageView = null;  
  24.       
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.           
  30.         urlPath = (EditText)findViewById(R.id.urlpath);  
  31.         button = (Button)findViewById(R.id.button);  
  32.         imageView = (ImageView)findViewById(R.id.imageView);  
  33.           
  34.         button.setOnClickListener(new View.OnClickListener() {  
  35.               
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 String urlPathContent = urlPath.getText().toString();  
  39.                 try {  
  40.                     byte[] data = ImageService.getImage(urlPathContent);  
  41.                     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  //生成位图  
  42.                     imageView.setImageBitmap(bitmap);   //显示图片  
  43.                 } catch (IOException e) {  
  44.                     Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show();  //通知用户连接超时信息  
  45.                     Log.i(TAG, e.toString());  
  46.                 }  
  47.                   
  48.             }  
  49.         });  
  50.     }  
  51. }  


3:ImageService类的内容

[java]  view plain copy
  1. package com.capinfotech.service;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7.   
  8. import com.capinfotech.utils.StreamTool;  
  9.   
  10. public class ImageService {  
  11.       
  12.     public static byte[] getImage(String path) throws IOException {  
  13.         URL url = new URL(path);  
  14.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  15.         conn.setRequestMethod("GET");   //设置请求方法为GET  
  16.         conn.setReadTimeout(5*1000);    //设置请求过时时间为5秒  
  17.         InputStream inputStream = conn.getInputStream();   //通过输入流获得图片数据  
  18.         byte[] data = StreamTool.readInputStream(inputStream);     //获得图片的二进制数据  
  19.         return data;  
  20.           
  21.     }  
  22. }  

4:StreamTool的内容

[java]  view plain copy
  1. package com.capinfotech.utils;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. public class StreamTool {  
  8.   
  9.     /* 
  10.      * 从数据流中获得数据 
  11.      */  
  12.     public static  byte[] readInputStream(InputStream inputStream) throws IOException {  
  13.         byte[] buffer = new byte[1024];  
  14.         int len = 0;  
  15.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  16.         while((len = inputStream.read(buffer)) != -1) {  
  17.             bos.write(buffer, 0, len);  
  18.         }  
  19.         bos.close();  
  20.         return bos.toByteArray();  
  21.           
  22.     }  
  23. }  

5:程序中用到的字符串资源strings.xml里的内容

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, MainActivity!</string>  
  4.     <string name="app_name">图片浏览器</string>  
  5.     <string name="urlpath">网络图片地址</string>  
  6.     <string name="button">显示</string>  
  7.     <string name="error">网络连接超时</string>  
  8. </resources>  

6:程序布局文件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="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/urlpath"  
  11.     />  
  12. <EditText   
  13.     android:id="@+id/urlpath"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     android:text="http://www.eoeandroid.com/data/attachment/forum/201107/18/142935bbi8d3zpf3d0dd7z.jpg"  
  17.     />  
  18. <Button  
  19.     android:id="@+id/button"  
  20.     android:layout_width="wrap_content"   
  21.     android:layout_height="wrap_content"   
  22.     android:text="@string/button"  
  23.     />  
  24. <ImageView  
  25.     android:id="@+id/imageView"  
  26.     android:layout_width="wrap_content"   
  27.     android:layout_height="wrap_content"   
  28. />  
  29. </LinearLayout>  

7:程序界面及测试结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值