android如何往SDCard中存取圖片

博客分類:Android

androidSDCard圖片存取 

引用

Java代碼 複製代碼 收藏代碼spinner.gif

  1. import java.io.File;  

  2. import java.io.FileNotFoundException;  

  3. import java.io.FileOutputStream;  

  4. import java.io.IOException;  

  5.   

  6. import android.graphics.Bitmap;  

  7. import android.os.Environment;  

  8. import android.util.Log;  

  9.   

  10. /** 

  11.  * 保存圖片的類 

  12.  *  

  13.  *  

  14.  */  

  15. public class SaveBitmap {  

  16.   

  17.     private final static String CACHE = "/css";  

  18.   

  19.     /** 

  20.      * 保存圖片的方法 保存到sdcard 

  21.      *  

  22.      * @throws Exception 

  23.      *  

  24.      */  

  25.     public static void saveImage(Bitmap bitmap, String imageName)  

  26.             throws Exception {  

  27.         String filePath = isExistsFilePath();  

  28.         FileOutputStream fos = null;  

  29.         File file = new File(filePath, imageName);  

  30.         try {  

  31.             fos = new FileOutputStream(file);  

  32.             if (null != fos) {  

  33.                 bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);  

  34.                 fos.flush();  

  35.                 fos.close();  

  36.             }  

  37.         } catch (FileNotFoundException e) {  

  38.             e.printStackTrace();  

  39.         } catch (IOException e) {  

  40.             e.printStackTrace();  

  41.         }  

  42.     }  

  43.   

  44.     /** 

  45.      * 獲取sd卡的緩存路徑, 一般在卡中sdCard就是這個目錄 

  46.      *  

  47.      * @return SDPath 

  48.      */  

  49.     public static String getSDPath() {  

  50.         File sdDir = null;  

  51.         boolean sdCardExist = Environment.getExternalStorageState().equals(  

  52.                 android.os.Environment.MEDIA_MOUNTED); // 判斷sd卡是否存在  

  53.         if (sdCardExist) {  

  54.             sdDir = Environment.getExternalStorageDirectory();// 獲取根目錄  

  55.         } else {  

  56.             Log.e("ERROR""沒有內存卡");  

  57.         }  

  58.         return sdDir.toString();  

  59.     }  

  60.   

  61.     /** 

  62.      * 獲取緩存文件夾目錄 如果不存在創建 否則則創建文件夾 

  63.      *  

  64.      * @return filePath 

  65.      */  

  66.     private static String isExistsFilePath() {  

  67.         String filePath = getSDPath() + CACHE;  

  68.         File file = new File(filePath);  

  69.         if (!file.exists()) {  

  70.             file.mkdirs();  

  71.         }  

  72.         return filePath;  

  73.     }  

  74.     /** 

  75.      * 獲取SDCard文件 

  76.      *  

  77.      * @return Bitmap 

  78.      */  

  79.     public static Bitmap getImageFromSDCard(String imageName) {  

  80.         String filepath = getSDPath() + CACHE  + "/" + imageName;  

  81.         File file = new File(filepath);  

  82.         if (file.exists()) {  

  83.             Bitmap bm = BitmapFactory.decodeFile(filepath);  

  84.             return bm;  

  85.         }  

  86.         return null;  

  87.     }  

  88. }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章