public class MainActivity extends Activity {
private Button button =null;
private ImageView image =null;
private EditText edit =null;
private Bitmap bitmap =null;//读取的网络图片
private Handler handler =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
InitView();
//添加按钮监听器
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//启动线程和网络资源交互,并使用handler与主线程界面交互
new Thread(new httprun()).start();
}
});
//Handler
handler =new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what ==1){
image.setImageBitmap(bitmap);
}
}
};
}
private void InitView() {
// TODO Auto-generated method stub
button=(Button)findViewById(R.id.btnView);
image=(ImageView)findViewById(R.id.ivImage);
edit=(EditText)findViewById(R.id.etImageUrl);
}
@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;
}
//访问网络图片的线程
class httprun implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
String str_url =edit.getText().toString().trim();
if(str_url!=null) {
try {
URL url =new URL(str_url);
HttpURLConnection hConnection =(HttpURLConnection) url.openConnection();//获得网络连接对象
hConnection.setConnectTimeout(5000);
hConnection.setRequestMethod("GET");
if(hConnection.getResponseCode() ==200){
InputStream in=hConnection.getInputStream();//获得输入流对象
//通过bitmap来封装图片
bitmap =BitmapFactory.decodeStream(in);
//通过handler通知主界面显示图片
handler.sendEmptyMessage(1);
}else {
Toast.makeText(MainActivity.this, "读取图片失败!", 1000).show();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
Toast.makeText(MainActivity.this, "url不能为空!", 3000).show();
}
}
}
}
private Button button =null;
private ImageView image =null;
private EditText edit =null;
private Bitmap bitmap =null;//读取的网络图片
private Handler handler =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
InitView();
//添加按钮监听器
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//启动线程和网络资源交互,并使用handler与主线程界面交互
new Thread(new httprun()).start();
}
});
//Handler
handler =new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what ==1){
image.setImageBitmap(bitmap);
}
}
};
}
private void InitView() {
// TODO Auto-generated method stub
button=(Button)findViewById(R.id.btnView);
image=(ImageView)findViewById(R.id.ivImage);
edit=(EditText)findViewById(R.id.etImageUrl);
}
@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;
}
//访问网络图片的线程
class httprun implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
String str_url =edit.getText().toString().trim();
if(str_url!=null) {
try {
URL url =new URL(str_url);
HttpURLConnection hConnection =(HttpURLConnection) url.openConnection();//获得网络连接对象
hConnection.setConnectTimeout(5000);
hConnection.setRequestMethod("GET");
if(hConnection.getResponseCode() ==200){
InputStream in=hConnection.getInputStream();//获得输入流对象
//通过bitmap来封装图片
bitmap =BitmapFactory.decodeStream(in);
//通过handler通知主界面显示图片
handler.sendEmptyMessage(1);
}else {
Toast.makeText(MainActivity.this, "读取图片失败!", 1000).show();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
Toast.makeText(MainActivity.this, "url不能为空!", 3000).show();
}
}
}
}