在android中显示网络图片及查看页面源代码

来源:互联网 发布:软件开发人月成本 编辑:程序博客网 时间:2024/06/01 13:22

 一:查看网络图片

1、用tomcat跑起一个web项目,记下图片路径:

这里的ip切记不要写成localhost或者127.0.0.1 ,写成局域网的。

2、搭建android项目

涉及的文件如下图红框内

(1)、MainActivity

/netimage/src/net/thinkeye/image/ImageMainActivity.java

  1. package net.thinkeye.image; 
  2.  
  3. import net.thinkeye.service.ImageService; 
  4. import android.os.Bundle; 
  5. import android.app.Activity; 
  6. import android.graphics.Bitmap; 
  7. import android.graphics.BitmapFactory; 
  8. import android.view.View; 
  9. import android.widget.Button; 
  10. import android.widget.EditText; 
  11. import android.widget.ImageView; 
  12. import android.widget.Toast; 
  13.  
  14. public class ImageMainActivity extends Activity { 
  15.      
  16.     private EditText pathText; 
  17.     private ImageView imageView; 
  18.  
  19.     @Override 
  20.     protected void onCreate(Bundle savedInstanceState) { 
  21.         super.onCreate(savedInstanceState); 
  22.         setContentView(R.layout.image_main); 
  23.         pathText = (EditText) this.findViewById(R.id.imagepath); 
  24.         imageView = (ImageView) this.findViewById(R.id.imageView); 
  25.         Button b = (Button)this.findViewById(R.id.button); 
  26.         b.setOnClickListener(new ButtonClickListener()); 
  27.     } 
  28.      
  29.     private final class ButtonClickListener implements View.OnClickListener{ 
  30.  
  31.         @Override 
  32.         public void onClick(View v) { 
  33.             String path = pathText.getText().toString(); 
  34.             try { 
  35.                 byte[] data = ImageService.getImage(path); 
  36.                 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
  37.                 imageView.setImageBitmap(bitmap);//显示图片 
  38.             } catch (Exception e) { 
  39.                 e.printStackTrace(); 
  40.                 Toast.makeText(getApplicationContext(), R.string.error, 1).show(); 
  41.             } 
  42.         } 
  43.          
  44.     } 
  45.  

2、

/netimage/src/net/thinkeye/service/ImageService.java

  1. package net.thinkeye.service; 
  2.  
  3. import java.io.InputStream; 
  4. import java.net.HttpURLConnection; 
  5. import java.net.URL; 
  6.  
  7. import net.thinkeye.utils.StreamTool; 
  8.  
  9. public class ImageService { 
  10.  
  11.     /** 
  12.      * 获取网络图片的数据 
  13.      * @param path 
  14.      * @return 
  15.      */ 
  16.     public static byte[] getImage(String path) throws Exception{ 
  17.         URL url = new URL(path); 
  18.         HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
  19.         conn.setConnectTimeout(5000); 
  20.         conn.setRequestMethod("GET"); 
  21.         if(conn.getResponseCode() == 200){ 
  22.             InputStream is = conn.getInputStream(); 
  23.             return StreamTool.read(is); 
  24.         } 
  25.         return null
  26.     } 
  27.  

3、/netimage/src/net/thinkeye/utils/StreamTool.java

  1. package net.thinkeye.utils; 
  2.  
  3. import java.io.ByteArrayOutputStream; 
  4. import java.io.InputStream; 
  5.  
  6. public class StreamTool { 
  7.  
  8.     /** 
  9.      * 读取流中的数据 
  10.      * @param is 
  11.      * @return 
  12.      * @throws Exception 
  13.      */ 
  14.     public static byte[] read(InputStream is) throws Exception{ 
  15.         ByteArrayOutputStream o = new ByteArrayOutputStream(); 
  16.         byte[] buffer = new byte[1024]; 
  17.         int len = 0
  18.         while((len = is.read(buffer)) != -1){ 
  19.             o.write(buffer,0,len); 
  20.         } 
  21.         is.close(); 
  22.         return o.toByteArray(); 
  23.     } 
  24.  

4、页面布局部分:

image_main.xml

  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.  
  7.     <TextView 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:text="@string/imagepath" /> 
  11.      
  12.     <EditText 
  13.         android:layout_width="fill_parent" 
  14.         android:layout_height="wrap_content" 
  15.         android:text="http://192.168.1.103:8888/SAMS/2.gif" 
  16.         android:id="@+id/imagepath" /> 
  17.      
  18.     <Button 
  19.         android:layout_width="wrap_content" 
  20.         android:layout_height="wrap_content" 
  21.         android:text="@string/button" 
  22.         android:id="@+id/button" /> 
  23.      
  24.     <ImageView  
  25.         android:layout_width="wrap_content" 
  26.         android:layout_height="wrap_content" 
  27.         android:id="@+id/imageView"/> 
  28.  
  29. </LinearLayout> 

5、定义字符常量

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="app_name">网络图片查看器</string> 
  4.     <string name="imagepath">网络图片路径</string> 
  5.     <string name="button">查看</string> 
  6.     <string name="error">获取图片失败</string> 
  7. </resources> 

6、在AndroidManifest.xml文件里加入访问网络的权限,因为这里涉及到上网流量,会向用户收取一定的费用,在默认情况下是没有这个权限的,需要在此文件中申请权限,如下图:

 

好了,tomcat跑起来后在浏览器中能看到图片后,就把这个应用部署到模拟器上了。

部署完成后,就到了如下的页面:

点击上图的 查看 按钮,即可查看网络中的图片

 

 

 

 

 

 二:查看页面源代码的功能和这个差不多,如下图:

 下面显示源代码的部分的TextView需用ScrollView包裹起来,

  1. <ScrollView  
  2.         android:layout_width="wrap_content" 
  3.         android:layout_height="wrap_content"> 
  4.          <TextView 
  5.             android:layout_width="fill_parent" 
  6.             android:layout_height="wrap_content" 
  7.             android:id="@+id/codeView" /> 
  8.     </ScrollView> 

 

 

 

 

 

 

本文出自 “新博客-http://cfei.net” 博客,请务必保留此出处http://johnny84.blog.51cto.com/2855387/1162457

原创粉丝点击