图片的远程加载

  1. package com.google.android.panoramio;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.util.Log;  
  6.   
  7. import java.io.BufferedInputStream;  
  8. import java.io.BufferedOutputStream;  
  9. import java.io.ByteArrayOutputStream;  
  10. import java.io.Closeable;  
  11. import java.io.IOException;  
  12. import java.io.InputStream;  
  13. import java.io.OutputStream;  
  14. import java.net.URL;  
  15.   
  16. /** 
  17.  *  从指定URL加载位图的工具类 
  18.  * 
  19.  */  
  20. public class BitmapUtils {  
  21.       
  22.     private static final String TAG = "Panoramio";  
  23.       
  24.     private static final int IO_BUFFER_SIZE = 4 * 1024;  
  25.       
  26.     /** 
  27.      * 从指定的url加载位图,这将会持续一段时间,因此不应该从UI线程中调用 
  28.      */  
  29.     public static Bitmap loadBitmap(String url) {  
  30.         Bitmap bitmap = null;  
  31.         InputStream in = null;  
  32.         BufferedOutputStream out = null;  
  33.   
  34.         try {  
  35.             in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);  
  36.   
  37.             final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();  
  38.             out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);  
  39.             copy(in, out);  
  40.             out.flush();  
  41.   
  42.             final byte[] data = dataStream.toByteArray();  
  43.             //调用BitmapFactory类的函数从字节数组中解码出位图  
  44.             bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
  45.         } catch (IOException e) {  
  46.             Log.e(TAG, "Could not load Bitmap from: " + url);  
  47.         } finally {  
  48.             closeStream(in);  
  49.             closeStream(out);  
  50.         }  
  51.   
  52.         return bitmap;  
  53.     }  
  54.   
  55.     /** 
  56.      * 关闭指定的数据流 
  57.      */  
  58.     private static void closeStream(Closeable stream) {  
  59.         if (stream != null) {  
  60.             try {  
  61.                 stream.close();  
  62.             } catch (IOException e) {  
  63.                 android.util.Log.e(TAG, "Could not close stream", e);  
  64.             }  
  65.         }  
  66.     }  
  67.   
  68.     /** 
  69.      * 使用临时的字节数组缓存将InputStream中的数据拷贝到OutputStream 
  70.      */  
  71.     private static void copy(InputStream in, OutputStream out) throws IOException {  
  72.         byte[] b = new byte[IO_BUFFER_SIZE];  
  73.         int read;  
  74.         while ((read = in.read(b)) != -1) {  
  75.             out.write(b, 0, read);  
  76.         }  
  77.     }  
  78.   

  79. 从其他博客看到的一段代码,里面的new URL(url).openStream()感觉很便利,记录一下增长下见识 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值