歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 如何以流的方式讀取圖片文件

Android 如何以流的方式讀取圖片文件

日期:2017/3/1 11:08:59   编辑:Linux編程

在讀取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); 代碼要多很多,

可為什麼要用這種方式,系統要提供這樣的方法呢?具體原因,我沒有深入學習,不過,網上有種說法,以流的方式讀取,可能更利於垃圾回收,不知道真假!

Copyright © Linux教程網 All Rights Reserved