Android 如何以流的方式读取图片文件


在读取sdcard中的图片文件时,如何以流的方式读取图片文件,请参阅下面的函数:

  1. public static void ShowImg(String uri, ImageView iv) throws IOException {     
  2.         FileInputStream fs = new FileInputStream(uri);  
  3.         BufferedInputStream bs = new BufferedInputStream(fs);  
  4.         Bitmap btp = BitmapFactory.decodeStream(bs);  
  5.         iv.setImageBitmap(btp);  
  6.         bs.close();  
  7.         fs.close();  
  8.         btp = null;       
  9. }  
要用到的类:java.io.FileInputStream,java.io.BufferedInputStream和Android.graphics.BitmapFactory

以流的方式读取要比直接以文件的方式读取:Bitmap btp = BitmapFactory.decodeFile(uri); 代码要多很多,

可为什么要用这种方式,系统要提供这样的方法呢?具体原因,我没有深入学习,不过,网上有种说法,以流的方式读取,可能更利于垃圾回收,不知道真假!

相关内容