首页 > 代码库 > Android获取网络图片
Android获取网络图片
/** * * 访问网络的操作,必须放在工作线程中完成 * */public class MainActivity extends Activity { static List<HashMap<String, String>> list; static List<String> userList2 = new ArrayList(); private static final int LOADSUCCESS=0x1; private static ImageView iv; private static TextView tv; private final myhandler handler=new myhandler(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv=(ImageView) findViewById(R.id.imageView1); new Thread(new Runnable() { public void run() { try { URL url=new URL("http://files.cnblogs.com/files/hyyweb/welcome.bmp"); InputStream in=url.openStream(); Bitmap bitmap=BitmapFactory.decodeStream(in); String result=""; Message message= handler.obtainMessage(LOADSUCCESS, bitmap); handler.sendMessage(message); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } private static class myhandler extends Handler{ private final WeakReference<MainActivity> weakReference; public myhandler(MainActivity mainActivity){ weakReference=new WeakReference<MainActivity>(mainActivity); } public void handleMessage(Message msg) { MainActivity mainActivity=weakReference.get(); if(mainActivity!=null){ switch (msg.what) { case LOADSUCCESS: MainActivity.iv.setImageBitmap((Bitmap) msg.obj); break; } } } }}
Android获取网络图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。