简单的网络图片查看
public void visit(View view) {
et_imageAdress = (EditText) findViewById(R.id.et_imageAdress);
final String imagetAdress = et_imageAdress.getText().toString();
imageView = (ImageView) findViewById(R.id.imageView);
if(TextUtils.isEmpty(imagetAdress)) {
Toast.makeText(this, "访问路径不能为空!", 0).show();
} else {
new Thread(){
public void run() {
try {
URL url = new URL(imagetAdress);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if(conn.getResponseCode()==200) {
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
//imageView.setImageBitmap(bitmap);
Message msg = Message.obtain();
msg.what = IMAGE_VIEW;
msg.obj = bitmap;
Log.i(Tag, "消息接收到了1。。。");
handler.sendMessage(msg);
}else {
Message msg = Message.obtain();
msg.what = OHTER_ERROR;
handler.sendMessage(msg);
//Toast.makeText(MainActivity.this, "123!", 0).show();
}
} catch (MalformedURLException e) {
//Toast.makeText(MainActivity.this, "访问路径错误!", 0).show();
Message msg = Message.obtain();
msg.what = PATH_ERROR;
handler.sendMessage(msg);
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
//Toast.makeText(MainActivity.this, "访问网络失败!", 0).show();
Message msg = Message.obtain();
msg.what = INTERNET_ERROR;
handler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
}
评论