Android实现网络图片查看器和网页源码查看器

网络图片查看器

清单文加入网络访问权限:

  1. |<!-- 访问internet权限 -->  
  2. <uses-permission android:name="android.permission.INTERNET"/>  

 

界面如下:


示例:

  1. <span style="FONT-WEIGHT: normal">public class MainActivity extends Activity {  
  2.     private EditText imagepath;  
  3.     private ImageView imageView;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.           
  9.         imagepath = (EditText) this.findViewById(R.id.imagepath);  
  10.         imageView = (ImageView) this.findViewById(R.id.imageView);  
  11.           
  12.         Button button = (Button) this.findViewById(R.id.button);  
  13.         button.setOnClickListener(new View.OnClickListener() {            
  14.             public void onClick(View v) {  
  15.                 String path = imagepath.getText().toString();  
  16.                 try{  
  17.                     byte[] data = ImageService.getImage(path);//获取图片数据  
  18.                     if(data!=null){  
  19.                         //构建位图对象  
  20.                         Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
  21.                         imageView.setImageBitmap(bitmap);//显示图片  
  22.                     }else{  
  23.                         Toast.makeText(getApplicationContext(), R.string.error, 1).show();  
  24.                     }                     
  25.                 }catch (Exception e) {  
  26.                     Toast.makeText(getApplicationContext(), R.string.error, 1).show();  
  27.                 }  
  28.             }  
  29.         });  
  30.     }  
  31. }</span>  
  1. <span style="FONT-WEIGHT: normal">public class ImageService {  
  2.     /**  
  3.      * 获取图片  
  4.      * @param path 网络图片路径  
  5.      * @return 图片的字节数据  
  6.      */  
  7.     public static byte[] getImage(String path) throws Exception{  
  8.         URL url = new URL(path);  
  9.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  10.         //设置超时时间  
  11.         conn.setConnectTimeout(5000);  
  12.         conn.setRequestMethod("GET");  
  13.         if(conn.getResponseCode()==200){  
  14.             InputStream inStream = conn.getInputStream();  
  15.             byte[] data = StreamTool.read(inStream);  
  16.             return data;  
  17.         }  
  18.         return null;  
  19.     }  
  20. }</span>  
  1. <span style="FONT-WEIGHT: normal">public class StreamTool {  
  2.     /**  
  3.      * 读取输入流数据  
  4.      * @param inStream  
  5.      * @return  
  6.      */  
  7.     public static byte[] read(InputStream inStream) throws Exception{  
  8.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  9.         byte[] buffer = new byte[1024];  
  10.         int len = 0;  
  11.         while( (len = inStream.read(buffer)) != -1 ){  
  12.             outStream.write(buffer, 0, len);  
  13.         }  
  14.         inStream.close();  
  15.         return outStream.toByteArray();  
  16.     }  
  17. }</span>  


网页源码查看器


如果网页的源码超过屏幕的显示位置的话,要求出现滚动条.

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

界面如下:

示例

  1.   @Override  
  2.   public void onCreate(Bundle savedInstanceState) {  
  3.       super.onCreate(savedInstanceState);  
  4.       setContentView(R.layout.main);  
  5.       pathText = (EditText) this.findViewById(R.id.path);  
  6.       htmlsource = (TextView) this.findViewById(R.id.htmlsource);  
  7.         
  8.       Button button = (Button) this.findViewById(R.id.button);  
  9.       button.setOnClickListener(new View.OnClickListener() {  
  10.       
  11.     public void onClick(View v) {  
  12.         String path = pathText.getText().toString();  
  13.         try{  
  14.             //获取源码  
  15.             String html = PageService.getHtml(path);  
  16.             htmlsource.setText(html);  
  17.         }catch (Exception e) {  
  18.             Toast.makeText(getApplicationContext(), R.string.error, 1).show();  
  19.         }  
  20.     }  
  21. });  
  22.   }  

  1. public class PageService {  
  2.     /**  
  3.      * 获取网页源代码  
  4.      * @param path 网页路径  
  5.      * @return  
  6.      */  
  7.     public static String getHtml(String path) throws Exception{  
  8.         URL url = new URL(path);  
  9.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  10.         conn.setConnectTimeout(5000);  
  11.         conn.setRequestMethod("GET");  
  12.         if(conn.getResponseCode() == 200){  
  13.             byte[] data = StreamTool.read(conn.getInputStream());  
  14.             return new String(data, "UTF-8");  
  15.         }  
  16.         return null;  
  17.     }  
  18. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值