Android中使用ImageView控件显示网络图片
来源:互联网 发布:天狼星期货软件 编辑:程序博客网 时间:2024/04/30 08:55
在android4.0以后的版本中,为了使得主界面流畅,所以设置了不允许在主线程中访问网络,为了安全,又不允许在其它线程中访问控件,这样就造成了ImageView等需要使用网络的控件更新时的问题,本文以Handler+Runnable的方式实现了ImageView控件显示网络图片.
在android4.0之后,如果在主线程中访问网络,会报一个android.os.networkonmainthreadexception的异常.因为在ui线程中访问网络的话,就要等待网络传输完成,期间就会阻塞线程,使界面卡住.
为了访问网络资源,可以开启一个线程,异步加载.但这个新线程又不具有更新控件的能力,所以就需要一个中间人来完成ui线程和网络线程的联系,本文使用的是消息机制,网络线程发送一个消息,ui线程中收到后执行相应工作.
package
com.example.netimage;
import
java.io.InputStream;
import
java.net.HttpURLConnection;
import
java.net.URL;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.Message;
import
android.app.Activity;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.view.Menu;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.ImageView;
public
class
MainActivity
extends
Activity {
private
Button b;
private
EditText et;
public
ImageView iv;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)
this
.findViewById(R.id.button1);
b.setOnClickListener(
new
ButtonOnClickListener());
iv = (ImageView)
this
.findViewById(R.id.imageView1);
et = (EditText)
this
.findViewById(R.id.editText1);
}
private
Handler handler =
new
Handler() {
public
void
handleMessage (Message msg) {
// 此方法在ui线程运行
switch
(msg.what) {
case
1
:
iv.setImageBitmap((Bitmap) msg.obj);
// iv 显示从网络获取到的logo
break
;
}
}
};
class
download
implements
Runnable{
@Override
public
void
run() {
// TODO Auto-generated method stub
String path = et.getText().toString();
try
{
//byte[] data = ImageService.getImage(path);
URL url =
new
URL(path);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setConnectTimeout(
5000
);
con.setRequestMethod(
"GET"
);
con.connect();
if
(con.getResponseCode() ==
200
){
InputStream is = con.getInputStream();
//byte[] data = StreamTool.readStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(is);
//(data, 0, data.length);
handler.obtainMessage(
1
,bitmap).sendToTarget();
}
//Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//ImageView iv = (ImageView)con.findViewById(R.id.imageView1);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private
class
ButtonOnClickListener
implements
View.OnClickListener{
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
new
Thread(
new
download()).start();
}
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
}
0 0
- Android中使用ImageView控件显示网络图片
- Android使用ImageView显示网络图片
- android中使用imageview显示Gif图片
- android中使用imageview显示Gif图片
- Android ImageView,布局或控件显示网络图片教程
- Android:ImageView如何显示网络图片
- Android:ImageView如何显示网络图片
- android用ImageView显示网络图片
- Android网络图片显示在ImageView 上面
- Android网络图片显示在ImageView上面
- ImageView显示网络图片
- Imageview显示网络图片
- android ImageView-在界面中显示图片
- Android如何使用ImageView加载网络图片
- Android 图片控件ImageView
- ImageView 使用网络图片
- ImageView显示图片控件介绍
- android 怎么把网络图片生成一个Bitmap显示在ImageView中
- 问题咨询
- 简述HLS,HTTP,RTSP,RTMP协议的区别
- Handler消息传递机制
- 485 繁琐问题,烦烦烦
- ndroid studio修改项目名,模块名,包名
- Android中使用ImageView控件显示网络图片
- JZOJ.1166 树中点对距离
- CocoaPods相关问题总结
- java OOM(OutOfMemory) 问题分析
- Android开源框架android-async-http的学习
- 鞍点
- [Android官方Demo系列] PageTransformer缩放滑动
- Hadoop HDFS读流程
- Android从ftp服务器获取文件