//儲存到SD卡 private static String sdState = Environment.getExternalStorageState(); private static String path = Environment.getExternalStorageDirectory().toString(); public static void saveBitmap(Bitmap bitmap,String imageName) { File file; File PicName; if(sdState.equals(Environment.MEDIA_MOUNTED)) { //獲得sd卡根目錄 file = new File(path + "/Huai/TicketsPic"); if(!file.exists()) { file.mkdirs(); } PicName = new File(file, imageName); try { if(!PicName.exists()) { PicName.createNewFile(); } FileOutputStream fos = new FileOutputStream(PicName); if(PicName.getName().endsWith(".png")) { bitmap.compress(CompressFormat.PNG, 100, fos); } else if(PicName.getName().endsWith(".jpg")) { bitmap.compress(CompressFormat.JPEG, 100, fos); } fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } //從SD卡取 public static Bitmap getBitmap(String imageName) { Bitmap bitmap = null; File imagePic; if(sdState.equals(Environment.MEDIA_MOUNTED)) { imagePic = new File(path + "/Huai/TicketsPic", imageName); if(imagePic.exists()) { try { bitmap = BitmapFactory.decodeStream(new FileInputStream(imagePic)); } catch (FileNotFoundException e) {// e.printStackTrace(); } } } return bitmap; } //將SD卡檔案刪除 public static void deleteFile(File file) { if(sdState.equals(Environment.MEDIA_MOUNTED)) { if (file.exists()) { if (file.isFile()) { file.delete(); } // 如果它是一個目錄 else if (file.isDirectory()) { // 聲明目錄下所有的檔案 files[]; File files[] = file.listFiles(); for (int i = 0; i < files.length; i++) { // 遍曆目錄下所有的檔案 deleteFile(files[i]); // 把每個檔案 用這個方法進行迭代 } } file.delete(); } } }