Android显示在线图片

来源:互联网 发布:双十一淘宝营销策略 编辑:程序博客网 时间:2024/04/28 07:29

转自:http://blog.csdn.net/ameyume/article/details/6106286


view plainprint?
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.InputStream;  
  3. import java.net.HttpURLConnection;  
  4. import java.net.URL;  
  5.   
  6. import android.app.Activity;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.ImageView;  
  14. import android.widget.Toast;  
  15.   
  16. public class AndroidTest extends Activity {  
  17.     private static final String URL = "http://avatar.csdn.net/3/2/4/2_ameyume.jpg";  
  18.     private EditText pathText;  
  19.     private ImageView imageView;  
  20.   
  21.     /** Called when the activity is first created. */  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.           
  27.         pathText = (EditText) this.findViewById(R.id.path);  
  28.         pathText.setText(URL);  
  29.         imageView = (ImageView) this.findViewById(R.id.imageView);  
  30.         Button button = (Button) this.findViewById(R.id.button);  
  31.         button.setOnClickListener(new View.OnClickListener() {  
  32.               
  33.             public void onClick(View v) {  
  34.                 String path = pathText.getText().toString();  
  35.                 try {  
  36.                     byte[] data = getImage(path);  
  37.                     if(data!=null){  
  38.                         Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// bitmap  
  39.                         imageView.setImageBitmap(bitmap);// display image  
  40.                     }else{  
  41.                         Toast.makeText(AndroidTest.this"Image error!"1).show();  
  42.                     }  
  43.                 } catch (Exception e) {  
  44.                     Toast.makeText(AndroidTest.this,"Newwork error!"1).show();  
  45.                     e.printStackTrace();  
  46.                 }  
  47.             }  
  48.         });  
  49.   
  50.     }  
  51.   
  52.     /** 
  53.      * Get image from newwork 
  54.      * @param path The path of image 
  55.      * @return 
  56.      * @throws Exception 
  57.      */  
  58.     public static byte[] getImage(String path) throws Exception{  
  59.         URL url = new URL(path);  
  60.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  61.         conn.setConnectTimeout(5 * 1000);  
  62.         conn.setRequestMethod("GET");  
  63.         InputStream inStream = conn.getInputStream();  
  64.         if(conn.getResponseCode()==200){  
  65.             return readStream(inStream);  
  66.         }  
  67.         return null;  
  68.     }  
  69.   
  70.     /** 
  71.      * Get data from stream 
  72.      * @param inStream 
  73.      * @return 
  74.      * @throws Exception 
  75.      */  
  76.     public static byte[] readStream(InputStream inStream) throws Exception{  
  77.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  78.         byte[] buffer = new byte[1024];  
  79.         int len = 0;  
  80.         while( (len=inStream.read(buffer)) != -1){  
  81.             outStream.write(buffer, 0, len);  
  82.         }  
  83.         outStream.close();  
  84.         inStream.close();  
  85.         return outStream.toByteArray();  
  86.     }  
  87.   

manifest.xml中增加网络权限
 

view plainprint?
  1. <uses-permission android:name="android.permission.INTERNET" />  


原创粉丝点击