Android开发学习:[22]获取网络图片

Android开发学习:[22]获取网络图片,本经验介绍Adroid如何通过新线程获取网络图片并显示到ImageView上。
本经验介绍Android如何通过新线程获取网络图片并显示到ImageView上。工具/原料AndroidStudio方法/步骤1首先我们打开我们下载安装好的AndroidStudio新建好项目步骤阅读2然后我们在布局界面中添加布局代码:主要就是在屏幕中央添加一个ImageView控件步骤阅读3然后我们编写一个getPicture()方法用来返回一个Bitmap对象,用来获取网络图片:  publicBitmapgetPicture(Stringpath){    Bitmapbm=null;    try{      URLurl=newURL(path);      URLConnectionconnection=url.openConnection();      connection.connect();      InputStreaminputStream=connection.getInputStream();      bm=BitmapFactory.decodeStream(inputStream);    }catch(MalformedURLExceptione){      e.printStackTrace();    }catch(IOExceptione){      e.printStackTrace();    }    return bm;  }步骤阅读4然后我们在oncreate方法中添加一个新线程: protectedvoidonCreate(BundlesavedInstanceState){    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main_activity3);    imageView=(ImageView)findViewById(R.id.imageView);    newThread(newRunnable(){      @Override      publicvoidrun(){        finalBitmapbitmap=getPicture(\"http://h.hiphotos.baidu.com/news/q%3D100/sign=be8e90f30cfa513d57aa68de0d6d554c/c75c10385343fbf29d10a30cb47eca8065388fe4.jpg\");        try{          Thread.sleep(2000);        }catch(InterruptedExceptione){          e.printStackTrace();        }        imageView.post(newRunnable(){          @Override          publicvoidrun(){            imageView.setImageBitmap(bitmap);          }        });      }    }).start();  }步骤阅读5编写好了之后我们打开虚拟机步骤阅读6然后运行此项目我们可以看到图片显示在屏幕中央步骤阅读END注意事项这是android系列经验,更多android问题请查看我的android系列经验Android开发学习(共41篇)上一篇:安卓查询联系人和...|下一篇:IntentService使...

本文来自投稿,不代表长河网立场,转载请注明出处: http://www.changhe99.com/a/qErq2exwJ7.html

(0)

相关推荐