android 实用例程记录

1.从网络下载图片并显示的例程

1:在UI线程中启动一个线程,让这个线程去下载图片。

2:图片完成下载后发送一个消息去通知UI线程

3:UI线程获取到消息后,更新UI。

executorService.submit(new Runnable(){
                @Override
                publicvoid run() { 


          try {


        URL newurl =new URL(params);
         HttpURLConnection conn = (HttpURLConnection)newurl.openConnection();
                        conn.setDoInput(true);
                        conn.connect(); 
                        InputStream inputStream=conn.getInputStream();
                        bitmap = BitmapFactory.decodeStream(inputStream); 
                        newhandler.post(new Runnable(){ 

          @Override
                            publicvoid run() { 
            ImageView view=(ImageView)frameLayout.findViewById(R.id.image);
                                view.setImageBitmap(bitmap);
                            }
                        }); 
                    } catch (MalformedURLException e) { 
                        e.printStackTrace();
                    } catch (IOException e) { 
                        e.printStackTrace();
                    } 
                }
                
            }); 
原文地址:https://www.cnblogs.com/Xiegg/p/3326214.html