Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 教學:讀取本地 SD 卡圖片的方法解析

編輯:關於 Android 程式設計

本文透過實例,說明 Android 開發中如何讀取本地 SD 卡上的圖片,分享給大家參考。

 private Bitmap getDiskBitmap(String pathString) { Bitmap bitmap = null; try { File file = new File(pathString); if(file.exists()) { bitmap = BitmapFactory.decodeFile(pathString); } } catch (Exception e) { // TODO: handle exception } return bitmap; } 

這個方法能從本地路徑讀取一張圖片,支援 jpg、bmp、png 等多種格式。其中 pathString 是本地圖片的路徑,例如 "mnt/sdcard/1.jpg"。

 File file = new File(pathString); if(file.exists()) {bitmap = BitmapFactory.decodeFile(pathString);} 

在讀取本地檔案後,建議先判斷該檔案是否存在,接著再呼叫 BitmapFactory.decodeFile(pathString); 對本地檔案進行解碼,它支援多種圖片格式,並會回傳一個 BITMAP 物件。

希望本文所述能對大家的 Android 程式設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved