查看网络上的图片

android4.0之后,在主线程中更新UI,子线程是可以向主线程中发送消息,告诉主线程任务和内容。利用到的类Handler    

主线程是不可以有长时间的等待的,超过一定的时间就会出错。这个例子是练习Handler消息处理机制

点击浏览按钮之后,就会加载一个网络图片   那个图片的地址可能不稳定。下次找个稳定的。

xml文件  activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FFFFFF"
    tools:context=".MainActivity" >
	<ImageView 
	    android:id="@+id/iv_imgs"
	    android:layout_weight="1000"
	    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="图片"
	    />
    <EditText
        android:id="@+id/et_path"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/url"
        android:hint="请输入图片路径" />
	<Button
	    android:onClick="click"
	    android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#698B22"
        android:text="浏览"
        android:textColor="#FFFFFF"
        />
</LinearLayout>
MainActivity.java 文件

public class MainActivity extends Activity {

	protected static final int CHANER_UI = 1;
	protected static final int ERROR = 2;
	private ImageView iv_img;
	private EditText et_path;
	@SuppressLint("HandlerLeak")
	private Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// 创建消息管理器
			if (msg.what==CHANER_UI) {
				Bitmap bitmap = (Bitmap) msg.obj;
				iv_img.setImageBitmap(bitmap);
			} else if(msg.what==ERROR){
				Toast.makeText(MainActivity.this, "显示图片失败", 0).show();
			}
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv_img = (ImageView) findViewById(R.id.iv_imgs);
		et_path = (EditText) findViewById(R.id.et_path);
	}

	public void click(View view){
		final String path = et_path.getText().toString().trim();
		if(TextUtils.isEmpty(path)){
			Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_SHORT).show();
		}else{
			new Thread(){
				public void run() {
					//连接服务器get请求获取图片。
					
					try {
						URL url=new URL(path);//内部类访问外部的变量  所以要把变量要用final进行修饰
						//根据url发送http的请求
						HttpURLConnection conn = (HttpURLConnection) url.openConnection();
						//设置请求的方式
						conn.setRequestMethod("GET");
						//设置连接超时时间  客户端和服务器的连接
						conn.setConnectTimeout(5000);
						//设置读取超时时间   已经显示了一点了  网络出现问题的情况   前提已经连接成功了。
						//conn.setReadTimeout(5000);
						//conn.setRequestProperty(field, newValue)
						//得到服务器返回的响应码  response  反应  回答  答复
						int  code = conn.getResponseCode();
						if(code == 200){
							InputStream  is =	conn.getInputStream();//得到InputStream的输入流  流里面就包含有信息
							//把一个流里面的内容显示出来。
							Bitmap bitmap = BitmapFactory.decodeStream(is);
							//利用消息管理器向消息队列发送信息 todo:告诉主线程一个消息  帮我更改界面 内容:bitmap
							
							Message  msg = new Message();
							msg.what = CHANER_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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值