Android之网络操作 - 从网络获取图片或网页

1.在Java中操作显示网络图片

  1. public class ImageRequest   
  2. {  
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) throws Exception   
  7.     {  
  8.         //构造一个URL对象  
  9.         URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");  
  10.         //使用openConnection打开URL对象  
  11.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  12.         //使用Http协议,设置请求方式为GET  
  13.         conn.setRequestMethod("GET");  
  14.         //设置链接超时异常,5s  
  15.         conn.setConnectTimeout(5 * 1000);  
  16.         //通过输入流获取图片数据  
  17.         InputStream inStream = conn.getInputStream();  
  18.         //获取到图片的二进制数据  
  19.         byte[] data = readInputStream(inStream);  
  20.         //构造一个文件,保存图片到项目的根目录下  
  21.         File imageFile = new File("baidu_logo.jpg");  
  22.         //构造一个文件输出流FileOutputStream  
  23.         FileOutputStream outStream = new FileOutputStream(imageFile);  
  24.         //把文件数据写入到输出流  
  25.         outStream.write(data);  
  26.         outStream.close();  
  27.     }  
  28.       
  29.     /** 
  30.      * 从输入流里面得到返回为二进制的数据 
  31.      * @param inStream 输入流 
  32.      * @return byte[] 二进制数据 
  33.      * @throws Exception 
  34.      */  
  35.     public static byte[] readInputStream(InputStream inStream) throws Exception  
  36.     {  
  37.         //构造一个ByteArrayOutputStream  
  38.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  39.         //设置一个缓冲区  
  40.         byte[] buffer = new byte[1024];  
  41.         int len = 0;  
  42.         //判断输入流长度是否等于-1,即非空  
  43.         while( (len=inStream.read(buffer)) != -1 )  
  44.         {  
  45.             //把缓冲区的内容写入到输出流中,从0开始读取,长度为len  
  46.             outStream.write(buffer, 0, len);  
  47.         }  
  48.         //关闭输入流  
  49.         inStream.close();  
  50.         return outStream.toByteArray();  
  51.     }  
  52. }  
 

 

2.Android中显示图片:

(1)ImageShowActivity.java

  1. public class ImageShowActivity extends Activity   
  2. {  
  3.     private static final String TAG = "ImageShowActivity";  
  4.     private EditText pathText;  
  5.     private ImageView imageView;  
  6.       
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState)   
  9.     {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.         //得到EditText,ImageView与Button  
  13.         pathText = (EditText) this.findViewById(R.id.urlpath);  
  14.         imageView = (ImageView) this.findViewById(R.id.imageView);          
  15.         Button button = (Button)this.findViewById(R.id.button);  
  16.         //设置Button监听  
  17.         button.setOnClickListener(new View.OnClickListener()   
  18.         {  
  19.             @Override  
  20.             public void onClick(View v)   
  21.             {  
  22.                 //获取EditText里面的地址  
  23.                 String path = pathText.getText().toString();  
  24.                 try   
  25.                 {  
  26.                     //得到图片的二进制数据  
  27.                     byte[] data = ImageService.getImage(path);  
  28.                     //图片处理  
  29.                     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//生成位图  
  30.                     //显示图片    
  31.                     imageView.setImageBitmap(bitmap);                 
  32.                 }   
  33.                 catch (Exception e)   
  34.                 {  
  35.                     //出错的时候提示错误信息  
  36.                     Toast.makeText(ImageShowActivity.this, R.string.error, 1).show();  
  37.                     //Log里面打印错误信息  
  38.                     Log.e(TAG, e.toString());  
  39.                 }  
  40.             }  
  41.         });  
  42.     }  
  43. }  
 

(2)编写一个流处理工具类,StreamTool.java

  1. public class StreamTool   
  2. {  
  3.     /** 
  4.      * 从输入流里面得到返回为二进制的数据 
  5.      * @param inStream 输入流 
  6.      * @return byte[] 二进制数据 
  7.      * @throws Exception 
  8.      */  
  9.     public static byte[] readInputStream(InputStream inStream) throws Exception  
  10.     {  
  11.         //构造一个ByteArrayOutputStream  
  12.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  13.         //设置一个缓冲区  
  14.         byte[] buffer = new byte[1024];  
  15.         int len = 0;  
  16.         //判断输入流长度是否等于-1,即非空  
  17.         while( (len=inStream.read(buffer)) != -1 )  
  18.         {  
  19.             //把缓冲区的内容写入到输出流中,从0开始读取,长度为len  
  20.             outStream.write(buffer, 0, len);  
  21.         }  
  22.         //关闭输入流  
  23.         inStream.close();  
  24.         return outStream.toByteArray();  
  25.     }  
  26. }  
 

(3)编写一个图片处理类,打开URL,获取输入流等操作

  1. public class ImageService   
  2. {     
  3.     public static byte[] getImage(String path) throws Exception   
  4.     {  
  5.         //构造一个URL对象  
  6.         URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");  
  7.         //使用openConnection打开URL对象  
  8.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  9.         //使用Http协议,设置请求方式为GET  
  10.         conn.setRequestMethod("GET");  
  11.         //设置链接超时异常,5s  
  12.         conn.setConnectTimeout(5 * 1000);  
  13.         //通过输入流获取图片数据  
  14.         InputStream inStream = conn.getInputStream();  
  15.         //返回图片的二进制数据  
  16.         return StreamTool.readInputStream(inStream);  
  17.     }  
  18. }  
 

注意别忘记在AndroidManifest.xml文件中添加访问网络的权限:

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

 

3.在Android中显示网页代码:通过滚动条视图(ScrollView)工具显示代码

(1)main.xml

  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.     <ScrollView  
  8.      android:layout_width="fill_parent"  
  9.      android:layout_height="fill_parent">  
  10.         <TextView    
  11.             android:layout_width="fill_parent"   
  12.             android:layout_height="wrap_content"   
  13.             android:id="@+id/textView"  
  14.         />  
  15.     </ScrollView>  
  16. </LinearLayout>  
 

(2)与上面显示图片类似

  1. public class MainActivity extends Activity   
  2. {  
  3.     /** Called when the activity is first created. */  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState)   
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.           
  10.         TextView textView = (TextView)this.findViewById(R.id.textView);  
  11.         try   
  12.         {  
  13.             textView.setText(HtmlService.getHtml("http://www.sohu.com"));  
  14.         }   
  15.         catch (Exception e)   
  16.         {  
  17.             Log.e("MainActivity", e.toString());  
  18.             Toast.makeText(MainActivity.this"网络连接失败"1).show();  
  19.         }  
  20.     }  
  21. }  
 

  1. public class HtmlService   
  2. {  
  3.     public static String getHtml(String path) throws Exception   
  4.     {  
  5.         URL url = new URL(path);  
  6.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  7.         conn.setRequestMethod("GET");  
  8.         conn.setConnectTimeout(5 * 1000);  
  9.         //通过输入流获取html数据  
  10.         InputStream inStream = conn.getInputStream();  
  11.         //得到html的二进制数据  
  12.         byte[] data = StreamTool.readInputStream(inStream);  
  13.         String html = new String(data, "gb2312");  
  14.         return html;  
  15.     }  
  16. }  
 

  1. public class StreamTool   
  2. {  
  3.     /** 
  4.      * 从输入流中获取数据 
  5.      * @param inStream 输入流 
  6.      * @return 
  7.      * @throws Exception 
  8.      */  
  9.     public static byte[] readInputStream(InputStream inStream) throws Exception  
  10.     {  
  11.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  12.         byte[] buffer = new byte[1024];  
  13.         int len = 0;  
  14.         while( (len=inStream.read(buffer)) != -1 )  
  15.         {  
  16.             outStream.write(buffer, 0, len);  
  17.         }  
  18.         inStream.close();  
  19.         return outStream.toByteArray();  
  20.     }  
  21. }  

  转载请标明出处 http://blog.csdn.net/shimiso 

技术交流群:66756039

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值