• 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;
                    }
                }
            }
        }
    
    }
  • 相关阅读:
    Tomcat造成404
    ajax缺少@ResponseBody注解前台404,业务可以运行
    几种常见的Runtime Exception
    SQL注入通俗讲解
    MYSQL数据库导入大数据量sql文件失败的解决方案
    css选择器
    http端口
    基础算法之最大子列求和问题
    基础算法之链表逆序
    Prolog&Epilog
  • 原文地址:https://www.cnblogs.com/hyyweb/p/6118167.html
Copyright © 2020-2023  润新知