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


网络图片查看器

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

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

界面如下:

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

示例:

  1. 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. }
 
  1. 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. }
 
  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. }
  • 1
  • 2
  • 下一页

相关内容