网络图片查看器Final
一、对于子线程更新ui操作通过消息传递给主线程的Handler 消息机制中:多个message处理 多个Message,通过msg.what来区分,msg.obj携带数据 protected static final int SET_IMAGE = 1; protected static final int ERROR = 2; protected static final int FAILED = 3; Message message = new Message();
message.what = SET_IMAGE;//是一个int类型常量
message.obj = bitmap;
handler.sendMessage(message);
Message msg = new Message();
msg.what = FAILED; handler.sendMessage(msg);Message msg = new Message();
msg.what = ERROR; handler.sendMessage(msg); |
核心代码: protected static final int SET_IMAGE = 1;
protected static final int ERROR = 2;
protected static final int FAILED = 3;
private EditText et_url;
private ImageView iv_image;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case ERROR:
Toast.makeText(MainActivity.this, "图片url不能为空。。。", 0).show();
break;
case SET_IMAGE:
Bitmap bitmap = (Bitmap) msg.obj;
iv_image.setImageBitmap(bitmap);
break;
case FAILED:
Toast.makeText(MainActivity.this, "获取图片失败", 0).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_image = (ImageView) this.findViewById(R.id.iv_image);
et_url = (EditText) this.findViewById(R.id.et_url);
}
/**
* 点击查看图片
*
* @param view
*/
public void viewImage(View view) {
new Thread() { // 重新创建一个Thread类处理请求网络的连接
@Override
public void run() {
// 原因: 访问网络 耗时的操作 需要一定的时间 为了避免界面卡死 无响应 4.0系统做了一个处理
// 所有的网络访问的操作 是不可以在主线程里面执行.
try {
// 1、获取用户输入的url
String url = et_url.getText().toString().trim();
if (TextUtils.isEmpty(url)) {
Looper.prepare();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
// Toast.makeText(MainActivity.this, "图片url不能为空。。。", 0).show();
return;
}
// 2、将地址包装成url
URL imageUrl = new URL(url);
// 3、获取一个HttpURLConnection
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
// 4、设置请求头信息
conn.setRequestMethod("GET");// 设置http的请求方式 get / post 注意单词
// 大写
conn.setConnectTimeout(5000);// 连接两秒超时
conn.setReadTimeout(5000);// 读取两秒超时
String userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)";
conn.setRequestProperty("User-Agent", userAgent);
// 5、获取服务器返回信息
int code = conn.getResponseCode();
if (code == 200) { // 返回正确码
InputStream in = conn.getInputStream();// 得到服务器返回的输入流
// iv_image.setImageBitmap(bitmap);
Bitmap bitmap = BitmapFactory.decodeStream(in);
Message message = new Message();
message.what = SET_IMAGE;
message.obj = bitmap;
handler.sendMessage(message);
} else {
// Toast.makeText(MainActivity.this, "请求失败", 0).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
Message msg = new Message();
msg.what = FAILED;
handler.sendMessage(msg);
// Toast.makeText(MainActivity.this, "获取图片失败", 0).show();
} catch (IOException e) {
e.printStackTrace();
Message msg = new Message();
msg.what = FAILED;
handler.sendMessage(msg);
// Toast.makeText(MainActivity.this, "获取图片失败", 0).show();
}
}
}.start();
}
} |
结果:
|