http://hi.baidu.com/programmar/blog/item/79483ecb2ac75cf552664fd3.html
在Android中显示一张网络图片其实是超级简单的,下面就一个非常简单的例子:
Step1:
1、创建你的Activity,本例中以ViewWebImageActivity说明;
2、ViewWebImageActivity中的代码如下:
String imageUrl = "http://hiphotos.baidu.com/baidu/pic/item/7d8aebfebf3f9e125c6008d8.jpg"; //这就是你需要显示的网络图片---网上随便找的
Bitmap bmImg;
ImageView imView;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imView = (ImageView) findViewById(R.id.imview);
imView.setImageBitmap(returnBitMap(imageUrl));
}
public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
3、其中,returnBitMap(String url) 方法就是具体实现网络图片转换成bitmap。
Step2:
1、修改你的main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
Step3:
1、在你的AndroidManifest.xml文件的</manifest>节点上面添加<uses-permission android:name="android.permission.INTERNET" />,这是由于Android有很多的权限限制,否则图片是不能在你的模拟器上显示的。
另附效果图片一张:
如此一来,你的目的就达到了,简单吧,那还不赶紧尝试!
分享到:
相关推荐
如何从网络中获取一张图片,并显示出来?? 首先应想到若要从网络资源中获取图片,就需要通过流操作,于是就想到如何创建流。 第一步:指定图片资源的URL 第二步:通过RUL获取一个connection 第三步:通过连接获取...
该文件可从网络指定位置下载图片,并设置图片在手机上的单点移动,多点触摸及放大缩小。
本文实例讲述了Android显示网络图片的方法,分享给大家供大家参考。具体方法如下: 一般来说,在Android中显示一张网络图片其实是非常简单的,下面就是一个非常简单的例子: 步骤1: ① 创建你的Activity,本例中以...
使用android studio开发显示网络图片,主要涉及HTTP协议,与数据流的知识点,对初学者研究如何从网络上获取图片,很有帮助
Android通过网络URL获取图片并显示
android中显示本地或网络GIF图片:通过Movie实现的,网络(Movie .decodeByteArray),本地(Movie.decodeStream)
android解析网络图片并显示在listview
android获取网络图片并显示
从网络加载图片显示到listview上,主函数布局是一个ListView item布局是TextView和ImageView 数量的多少视情况而定 再写一个网络请求数据的类 最后面有代码: ”urlConnection 请求数据类的封装“ 类名一定要一样...
android 获取网络图片 通过HttpURLConnection获取网络图片bitmap 并将图片显示到imageView中
Android布局或控件显示网络图片教程源码,文章详情访问:http://blog.csdn.net/ausboyue/article/details/52120649
android网络获取图片,保存到SD卡中,将网络图片或者本地图片在ViewPager中显示
详情查看:https://blog.csdn.net/automoblie0/article/details/122646237
利用android开源库android-gif-drawable和android-async-http不仅能显示本地的gif图片,还能从网络上获取Gif图片并显示.即使是较大的gif图片,也不会报OOM异常。
android快速开发框架afinal的特点: 使用简单,无需配置但可配置 在listview和gridview等容器加载图片的时候快速滑动无错误现象 支持线程池并发数量配置 支持内存缓存大小配置 支持磁盘大小缓存配置,和缓存...
通过图片的url获取到图片,并显示到imageview中,本实例中选择的图片的百度官网的logo。仅提供一种获取网络图片的方法。