//加载远程图片 public Bitmap getURLimage(String url) { Bitmap bmp = null; try { URL myurl = new URL(url); // 获得连接 HttpURLConnection conn = (HttpURLConnection) myurl.openConnection(); conn.setConnectTimeout(6000);//设置超时 conn.setDoInput(true); conn.setUseCaches(false);//不缓存 conn.connect(); InputStream is = conn.getInputStream();//获得图片的数据流 bmp = BitmapFactory.decodeStream(is); is.close(); } catch (Exception e) { e.printStackTrace(); } return bmp; }
调用
lv = (ListView) findViewById(R.id.listview); img = findViewById(R.id.imageView); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int position,long id) { TextView tv1= view.findViewById(R.id.tv_1); TextView tv2= findViewById(R.id.tv1); Urlimg="http://***********************/0000000001/s/"+tv1.getText().toString(); // 缩小图片 Urlimg="http://***********************/0000000001/"+tv1.getText().toString(); // 原始图片 tv2.setText(Urlimg); //新建线程加载图片信息,发送到消息队列中 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Bitmap bmp = getURLimage(Urlimg); Message msg = new Message(); msg.what = 1; msg.obj = bmp; System.out.println("000"); handle.sendMessage(msg); } }).start(); } });
//在消息队列中实现对控件的更改 private Handler handle = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: System.out.println("111"); Bitmap bmp=(Bitmap)msg.obj; img.setImageBitmap(bmp); break; } }; };
目前这段程序用在我的程序里了
发表评论