zoukankan      html  css  js  c++  java
  • Android 网络显示图片

    在Android中显示一张网络图片其实是超级简单的,下面就一个非常简单的例子:

    Step1:
    1、创建你的Activity,本例中以ViewWebImageActivity说明;
    2、ViewWebImageActivity中的代码如下:

    String urlString ="http://s7.sinaimg.cn/middle/9b82a8c54c10ecacbb686&960";
    private ImageView imageView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    imageView = (ImageView)findViewById(R.id.imageview01);
    try {
    imageView.setImageBitmap(returnBitMap(urlString));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }

    private Bitmap returnBitMap(String url) throws IOException{
    Bitmap bm = null;
    URL myFileUrl = null;
    try {
    myFileUrl = new URL(url);

    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
    conn.setDoInput(true);
    conn.connect();
    InputStream in = conn.getInputStream();
    bm = BitmapFactory.decodeStream(in);
    in.close();

    return bm;

    }

    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有很多的权限限制,否则图片是不能在你的模拟器上显示的。
  • 相关阅读:
    QTP模拟鼠标和键盘事件整理
    Linux 入门常用命令 — 改变文件或目录的访问权限
    做一个有品质的男人
    Linux下.tar .gz .tgz .bz2 .bz等解、压包命令详解
    全面整理CentOS系统使用中文
    MSDN宝藏库中,初学者应该看的东西【整理的很辛苦哦】
    IIS 7.5版本中一些诡异问题的解决方案
    老生常谈ASP.NET中的Cookies,罗列读写Cookies的方法
    分享3段平时很实用的微代码,高手莫喷
    SQL 2005中的临时表
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2522558.html
Copyright © 2011-2022 走看看