Android ImageView如何加载网络和本地图片资源

该博客展示了如何在Android应用中使用ImageView加载网络和本地图片资源。通过创建URL对象,建立HttpURLConnection连接,设置超时时间并获取输入流,然后使用BitmapFactory.decodeStream解析流为Bitmap对象,最后将Bitmap显示在ImageView上。
  1. package com.android.antking.imageview;  
  2.   
  3. import java.io.InputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.MalformedURLException;  
  6. import java.net.URL;  
  7.   
  8. import android.app.Activity;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import android.widget.ImageView;  
  15.   
  16. public class MainActivity extends Activity {  
  17.     //定义一个图片显示控件  
  18.     private ImageView imageView;  
  19.     /** Called when the activity is first created. */  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         //图片资源  
  25.         String url = "http://s16.sinaimg.cn/orignal/89429f6dhb99b4903ebcf&690";  
  26.         //得到可用的图片  
  27.         Bitmap bitmap = getHttpBitmap(url);  
  28.         imageView = (ImageView)this.findViewById(R.id.imageViewId);  
  29.         //显示  
  30.         imageView.setImageBitmap(bitmap);  
  31.          
  32.     }  
  33.     /** 
  34.      * 获取网落图片资源  
  35.      * @param url 
  36.      * @return 
  37.      */  
  38.     public static Bitmap getHttpBitmap(String url){  
  39.         URL myFileURL;  
  40.         Bitmap bitmap=null;  
  41.         try{  
  42.             myFileURL = new URL(url);  
  43.             //获得连接  
  44.             HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();  
  45.             //设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制  
  46.             conn.setConnectTimeout(6000);  
  47.             //连接设置获得数据流  
  48.             conn.setDoInput(true);  
  49.             //不使用缓存  
  50.             conn.setUseCaches(false);  
  51.             //这句可有可无,没有影响  
  52.             //conn.connect();  
  53.             //得到数据流  
  54.             InputStream is = conn.getInputStream();  
  55.             //解析得到图片  
  56.             bitmap = BitmapFactory.decodeStream(is);  
  57.             //关闭数据流  
  58.             is.close();  
  59.         }catch(Exception e){  
  60.             e.printStackTrace();  
  61.         }  
  62.           
  63.         return bitmap;  
  64.           
  65.     }  
  66. }  

从本地获取Bitmap对象方法
1.将文件转化为bitmap对象
public static final float DISPLAY_WIDTH = 200;
public static final float DISPLAY_HEIGHT = 200;
/**
 * 从path中获取图片信息
 * @param path
 * @return
 */
private Bitmap decodeBitmap(String path){
  BitmapFactory.Options op = new BitmapFactory.Options();
  //inJustDecodeBounds 
  //If set to true, the decoder will return null (no bitmap), but the out…
  op.inJustDecodeBounds = true;
  Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
  //获取比例大小
  int wRatio = (int)Math.ceil(op.outWidth/DISPLAY_WIDTH);
  int hRatio = (int)Math.ceil(op.outHeight/DISPLAY_HEIGHT);
  //如果超出指定大小,则缩小相应的比例
  if(wRatio > 1 && hRatio > 1){
    if(wRatio > hRatio){
      op.inSampleSize = wRatio;
    }else{
      op.inSampleSize = hRatio;
    }
  }
  op.inJustDecodeBounds = false;
  bmp = BitmapFactory.decodeFile(path, op);
  return bmp;
}
在通过BitmapFactory.decodeFile(String path)方法将突破转成Bitmap时,遇到大一些的图片,我们经常会遇到OOM(Out Of Memory)的问题。所以用到了我们上面提到的BitmapFactory.Options这个类。
2.通过二进制流Stream来获取

Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null,  bmpFactoryOptions);

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值