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

2024-10-31 12:05:02

1、首先我们打开我们下载安装好的Android Studio新建好项目

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

3、然后我们编写一个getPicture()方法用来返回一个Bitmap对象,用来获取网络图片: public Bitmap getPicture(String path){ Bitmap bm=null; try{ URL url=new URL(path); URLConnection connection=url.openConnection(); connection.connect(); InputStream inputStream=connection.getInputStream(); bm= BitmapFactory.decodeStream(inputStream); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bm; }

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

5、编写好了之后我们打开虚拟机

Android开发学习:[22]获取网络图片
猜你喜欢