安卓笔记之网络图片查看器小程序以及消息处理机制运用

来源:互联网 发布:什么漫画软件好 编辑:程序博客网 时间:2024/05/17 02:12

1.网络图片查看

在布局文件中添加一个ImageView EditText  Button

其中一个小细节

<!-- 当width和height都为fill_parent时,weight表示的是渲染的优先级,数字越大,优先级越低 -->
    <ImageView
        android:id="@+id/iv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="30"
         />


按钮点击事件

public void click(View v) {
// 获得网络路径
String path = et.getText().toString().trim();
System.out.println(path+"87");
if (TextUtils.isEmpty(path)) {
Toast.makeText(MainActivity.this, "地址不能为空", 0).show();
}else{
try {
URL url = new URL(path);
//根据url发送http请求
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//设置请求的方式
conn.setRequestMethod("GET");//此处大写
//设置最大请求时间为5s
conn.setConnectTimeout(5000);
//conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");

//获得请求码
int code=conn.getResponseCode();
if(code==200){//如果请求码为200,则请求成功
//获得conn的输入流
InputStream in=conn.getInputStream();

//将输入流转换成图片
Bitmap bitmap=BitmapFactory.decodeStream(in);
//imageview加载bitmap
iv.setImageBitmap(bitmap);

}else{
Toast.makeText(MainActivity.this, "图片链接失败", 0).show();
}

} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "未连接上", 0).show();
}
}


}


上述代码在4.0版本以下可以正常运行,但在4.0版本上运行会报以下错误



2.ANR

application not response

应用程序无响应

在4.0以上版本无法在主线程(UI)线程上运行网络请求应用。

避免anr:把所有耗时的操作放在子线程里执行。

3.为了使上面的程序能在4.0以上版本运行,需引入消息处理机制:

思路如下:



更改后的代码:

public class MainActivity extends Activity {
protected static final int CHANGE_UI = 1;
protected static final int ERROR = 2;
private ImageView iv;
private EditText et;


// 主线程创建handle
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == CHANGE_UI) {
Bitmap bitmap = (Bitmap) msg.obj;
iv.setImageBitmap(bitmap);
} else if (msg.what == ERROR) {
Toast.makeText(MainActivity.this, "不能显示图片", Toast.LENGTH_SHORT)
.show();
}
};
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
et = (EditText) findViewById(R.id.et);
}


public void click(View v) {
// 获得网络路径
final String path = et.getText().toString().trim();
System.out.println(path + "87");
if (TextUtils.isEmpty(path)) {
Toast.makeText(MainActivity.this, "地址不能为空", 0).show();
} else {
new Thread() {
public void run() {
try {
URL url = new URL(path);
// 根据url发送http请求
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
// 设置请求的方式
conn.setRequestMethod("GET");
// 设置最大请求时间为5s
conn.setConnectTimeout(5000);
// conn.setRequestProperty("User-Agent",
// "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");


// 获得请求码
int code = conn.getResponseCode();
if (code == 200) {// 如果请求码为200,则请求成功
// 获得conn的输入流
InputStream in = conn.getInputStream();


// 将输入流转换成图片
Bitmap bitmap = BitmapFactory.decodeStream(in);
// imageview加载bitmap
// iv.setImageBitmap(bitmap);
// 通知主线程,我要更新UI了
Message msg = new Message();
msg.what = CHANGE_UI;
msg.obj = bitmap;
handler.sendMessage(msg);


} else {
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}


} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
};
}.start();
}


}
}


0 0
原创粉丝点击