犀渠玉剑良家子,白马金羁侠少年。这篇文章主要讲述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获取网络图片】
推荐阅读
- Android菜鸟成长记11 -- sqlite数据库的设计和升降级
- Android Fragment使用 基础篇 温故知新
- android Service学习
- Android Fragment使用 嵌套Fragments (Nested Fragments) 的使用及常见错误
- Android SQLiteOpenHelper
- Android Fragment使用 Activity, Fragment, WebView的状态保存和恢复
- 仿知乎Android端回答UI
- android recyclerview的浅析。
- Android自动化测试手段之Monkey(adb shell monkey)