网络图片查看器

  1 public class MainActivity extends Activity implements OnClickListener {
  2 
  3     private static final String TAG = "MainActivity";
  4     protected static final int ERROR = 1;
  5     private EditText etUrl;
  6     private ImageView ivIcon;
  7     private final int SUCCESS = 0;
  8     
  9     private Handler handler = new Handler() {
 10 
 11         /**
 12          * 接收消息
 13          */
 14         @Override
 15         public void handleMessage(Message msg) {
 16             super.handleMessage(msg);
 17             
 18             Log.i(TAG, "what = " + msg.what);
 19             if(msg.what == SUCCESS) {    // 当前是访问网络, 去显示图片
 20                 ivIcon.setImageBitmap((Bitmap) msg.obj);        // 设置imageView显示的图片
 21             } else if(msg.what == ERROR) {
 22                 Toast.makeText(MainActivity.this, "抓取失败", 0).show();
 23             }
 24         }
 25     };
 26 
 27     @Override
 28     protected void onCreate(Bundle savedInstanceState) {
 29         super.onCreate(savedInstanceState);
 30         setContentView(R.layout.activity_main);
 31         
 32         ivIcon = (ImageView) findViewById(R.id.iv_icon);
 33         etUrl = (EditText) findViewById(R.id.et_url);
 34         
 35         findViewById(R.id.btn_submit).setOnClickListener(this);
 36     }
 37 
 38     @Override
 39     public void onClick(View v) {
 40         final String url = etUrl.getText().toString();
 41         
 42         new Thread(new Runnable() {
 43 
 44             @Override
 45             public void run() {
 46                 Bitmap bitmap = getImageFromNet(url);
 47 
 48 //                ivIcon.setImageBitmap(bitmap);        // 设置imageView显示的图片
 49                 if(bitmap != null) {
 50                     Message msg = new Message();
 51                     msg.what = SUCCESS;
 52                     msg.obj = bitmap;
 53                     handler.sendMessage(msg);
 54                 } else {
 55                     Message msg = new Message();
 56                     msg.what = ERROR;
 57                     handler.sendMessage(msg);
 58                 }
 59             }}).start();
 60         
 61     }
 62 
 63     /**
 64      * 根据url连接取网络抓去图片返回
 65      * @param url
 66      * @return url对应的图片
 67      */
 68     private Bitmap getImageFromNet(String url) {
 69         HttpURLConnection conn = null;
 70         try {
 71             URL mURL = new URL(url);    // 创建一个url对象
 72             
 73             // 得到http的连接对象
 74             conn = (HttpURLConnection) mURL.openConnection();
 75             
 76             conn.setRequestMethod("GET");        // 设置请求方法为Get
 77             conn.setConnectTimeout(10000);        // 设置连接服务器的超时时间, 如果超过10秒钟, 没有连接成功, 会抛异常
 78             conn.setReadTimeout(5000);        // 设置读取数据时超时时间, 如果超过5秒, 抛异常
 79             
 80             conn.connect();        // 开始链接
 81             
 82             int responseCode = conn.getResponseCode(); // 得到服务器的响应码
 83             if(responseCode == 200) {
 84                 // 访问成功
 85                 InputStream is = conn.getInputStream();    // 获得服务器返回的流数据
 86                 
 87                 Bitmap bitmap = BitmapFactory.decodeStream(is); // 根据 流数据 创建一个bitmap位图对象
 88                 
 89                 return bitmap;
 90             } else {
 91                 Log.i(TAG, "访问失败: responseCode = " + responseCode);
 92             }
 93         } catch (Exception e) {
 94             e.printStackTrace();
 95         } finally {
 96             if(conn != null) {
 97                 conn.disconnect();        // 断开连接
 98             }
 99         }
100         return null;
101     }
102 }
原文地址:https://www.cnblogs.com/xiaoying1245970347/p/4452844.html