源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android显示网络图片实例

  • 时间:2020-09-01 12:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android显示网络图片实例
本文实例讲述了Android显示网络图片的方法,分享给大家供大家参考。具体方法如下: 一般来说,在Android中显示一张网络图片其实是非常简单的,下面就是一个非常简单的例子: 步骤1: ① 创建你的Activity,本例中以ViewWebImageActivity说明; ② ViewWebImageActivity中的代码如下:
[u]复制代码[/u] 代码如下:
String imageUrl = "http://www.1sucai.cn/images/logo.gif"; //这就是你需要显示的网络图片---网上随便找的 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; }
③ 其中,returnBitMap(String url) 方法就是具体实现网络图片转换成bitmap。 步骤2: 修改你的main.xml文件如下:
[u]复制代码[/u] 代码如下:
<?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>
步骤3: 在你的AndroidManifest.xml文件的节点上面添加,这是由于Android有很多的权限限制,否则图片是不能在你的模拟器上显示的。 希望本文所述对大家的Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部