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

简介: 网络的访问在我们日常生活中太重要了,如果没有网络我们的生活将会是什么样子呢?android手机和浏览器也是一样的,也可以通过网络通讯获取数据,如调用webservice,EJB等。
+关注继续查看

网络的访问在我们日常生活中太重要了,如果没有网络我们的生活将会是什么样子呢?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>  

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


相关文章
|
1天前
|
传感器 存储 移动开发
SAP UI5 应用开发教程之五十 - 如何使用 Cordova 将 SAP UI5 应用生成一个能在 Android 手机上安装的混合应用试读版
SAP UI5 应用开发教程之五十 - 如何使用 Cordova 将 SAP UI5 应用生成一个能在 Android 手机上安装的混合应用试读版
16 1
|
10天前
|
Java 数据库 Android开发
性能提示-流畅运行的Android应用
性能提示-流畅运行的Android应用
7 0
|
19天前
|
设计模式 Android开发 uml
责任链模式以及在 Android 中的应用
责任链模式以及在 Android 中的应用
|
19天前
|
物联网 Android开发 芯片
我也是可以开发安卓蓝牙应用的男人了
我也是可以开发安卓蓝牙应用的男人了
|
2月前
|
Java Shell 数据处理
Android 根目录下和应用目录下的build.gradle的详解,以及groovy语法的讲解
Android 根目录下和应用目录下的build.gradle的详解,以及groovy语法的讲解
35 0
|
2月前
|
XML API Android开发
Android 实现APP内应用更新功能(支持Android7.0以上)
Android 实现APP内应用更新功能(支持Android7.0以上)
40 0
|
2月前
|
存储 开发工具 Android开发
Android 中内部存储和外部存储的理解与应用
Android 中内部存储和外部存储的理解与应用
38 0
|
2月前
|
Android开发
快应用安卓机进行真机调试(详细流程)
快应用安卓机进行真机调试(详细流程)
31 0
|
2月前
|
Android开发 Windows
Mac 好用的 Android 模拟器整理(玩游戏、装应用、支持咸鱼、拼多多...)
Mac 好用的 Android 模拟器整理(玩游戏、装应用、支持咸鱼、拼多多...)
661 47
|
3月前
|
Java API Android开发
Android项目实战:使用Retrofit构建天气预报应用
1. 项目简介 在这个Android项目实战中,我们将构建一个简单的天气预报应用。用户可以输入城市名称,获取该城市的实时天气信息、未来几天的天气预报以及其他相关信息。为了实现这个功能,我们将使用Retrofit框架进行网络请求,从OpenWeatherMap API获取天气数据。
76 0
相关产品
云迁移中心
推荐文章
更多