用Android的imageVew显示网络和本地的图片

关于imageview的说明就不谈了,可以去看看官方API介绍,里面很详细,这里我直接上实例代码了

 首先你要准备一张图片放到如下:

113119_ITBh_2391602.png

MainActivity.java的代码如下:

package com.yaowen.imageviewtest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
    private ImageView imageView;//显示图片的组件
    private Button netDownload, localDownload;//网络加载和本地加载按钮
    //服务器上得图片地址
    private String url = "http://e.hiphotos.baidu.com/image/pic/item/730e0cf3d7ca7bcb6e7f28c3ba096b63f724a8d2.jpg";
    //本地sd卡上的图片路径
    private String localPath = Environment.getExternalStorageDirectory() + "/Pictures/bg2.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        //初始化组件
        imageView = (ImageView) findViewById(R.id.imageTest);
        netDownload = (Button) findViewById(R.id.netDownload);
        localDownload = (Button) findViewById(R.id.localDownload);
        //调试语句,可以忽略
        Log.d("TAG", "localPath>>>>>" + localPath);
        //网络加载按钮监听事件
        netDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = null;//新建一个bitmap对象并设置其为空值
                bitmap = getHttpBitmap(url);//通过getHttpBitmap方法获得bitmap实例
                imageView.setImageBitmap(bitmap);//设置imageView的显示
            }
        });
        //本地加载按钮监听事件
        localDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FileInputStream fis = new FileInputStream(localPath);
                    //Bitmap bitmap = getLocalBitmap(localPath);
                    Bitmap bitmap = BitmapFactory.decodeStream(fis);
                    imageView.setImageBitmap(bitmap);
                    fis.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
   /* private Bitmap getLocalBitmap(String localPath) {
        try {
            fis = new FileInputStream(localPath);
            return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }*/
    /**
     * 从服务器获取图片资源
     *
     * @param url 服务器上图片的链接地址
     */
    private Bitmap getHttpBitmap(String url) {
        URL myUrl = null;
        Bitmap bitmap = null;
        Log.d("TAG", url);
        try {
            myUrl = new URL(url);
            //新建一个HttpURLConnection连接
            HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
            conn.setConnectTimeout(2000);//设置HttpURLConnection连接超时,时间为2S
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
            //conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}
<!--主布局文件代码-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/netDownload"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="网上加载" />
        <Button
            android:id="@+id/localDownload"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="本地加载" />
    </LinearLayout>
    <ImageView
        android:id="@+id/imageTest"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
<!--记得在manifest里加入这两个权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

最后附上图片:(以上图片只是示例啊,不要纠结啊,都是在网络上找的图片,可以随意改变得啦;)

112718_uFcn_2391602.png

112811_LhSe_2391602.png

 


温习提示:如果上面代码运行出错,而且有说到StrictMode的话,请看我的这边篇文章吧

http://my.oschina.net/yaowen424/blog/528542

转载于:https://my.oschina.net/yaowen424/blog/532598

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值