reverse_xiaoyu

忘记并不可怕,可怕的是你从来就都不知道!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

  1 public class SDCardHelper {
  2  
  3      // 判断SD卡是否被挂载
  4      public static boolean isSDCardMounted() {
  5          // return Environment.getExternalStorageState().equals("mounted");
  6          return Environment.getExternalStorageState().equals(
  7          Environment.MEDIA_MOUNTED);
  8     }
  9  
 10     // 获取SD卡的根目录
 11     public static String getSDCardBaseDir() {
 12          if (isSDCardMounted()) {
 13                return Environment.getExternalStorageDirectory().getAbsolutePath();
 14          }
 15          return null;
 16     }
 17  
 18     // 获取SD卡的完整空间大小,返回MB
 19     public static long getSDCardSize() {
 20          if (isSDCardMounted()) {
 21               StatFs fs = new StatFs(getSDCardBaseDir());
 22               long count = fs.getBlockCountLong();
 23               long size = fs.getBlockSizeLong();
 24               return count * size / 1024 / 1024;
 25          }
 26          return 0;
 27     }
 28  
 29     // 获取SD卡的剩余空间大小
 30     public static long getSDCardFreeSize() {
 31          if (isSDCardMounted()) {
 32                StatFs fs = new StatFs(getSDCardBaseDir());
 33                long count = fs.getFreeBlocksLong();
 34                long size = fs.getBlockSizeLong();
 35                return count * size / 1024 / 1024;
 36          }
 37          return 0;
 38     }
 39  
 40     // 获取SD卡的可用空间大小
 41     public static long getSDCardAvailableSize() {
 42          if (isSDCardMounted()) {
 43                StatFs fs = new StatFs(getSDCardBaseDir());
 44                long count = fs.getAvailableBlocksLong();
 45                long size = fs.getBlockSizeLong();
 46                return count * size / 1024 / 1024;
 47          }
 48          return 0;
 49     }
 50  
 51     // 往SD卡的公有目录下保存文件
 52     public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String fileName) {
 53          BufferedOutputStream bos = null;
 54          if (isSDCardMounted()) {
 55                File file = Environment.getExternalStoragePublicDirectory(type);
 56                try {
 57                     bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
 58                     bos.write(data);
 59                     bos.flush();
 60                     return true;
 61                } catch (Exception e) {
 62                     e.printStackTrace();
 63                } finally {
 64                     try {
 65                           bos.close();
 66                     } catch (IOException e) {
 67                           // TODO Auto-generated catch block
 68                           e.printStackTrace();
 69                     }
 70                }
 71           }
 72           return false;
 73      }
 74  
 75      // 往SD卡的自定义目录下保存文件
 76      public static boolean saveFileToSDCardCustomDir(byte[] data, String dir, String fileName) {
 77           BufferedOutputStream bos = null;
 78           if (isSDCardMounted()) {
 79                 File file = new File(getSDCardBaseDir() + File.separator + dir);
 80                 if (!file.exists()) {
 81                       file.mkdirs();// 递归创建自定义目录
 82                 }
 83                 try {
 84                       bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
 85                       bos.write(data);
 86                       bos.flush();
 87                       return true;
 88                 } catch (Exception e) {
 89                       e.printStackTrace();
 90                 } finally {
 91                       try {
 92                             bos.close();
 93                       } catch (IOException e) {
 94                             // TODO Auto-generated catch block
 95                             e.printStackTrace();
 96                       }
 97                 }
 98            }
 99            return false;
100      }
101  
102      // 往SD卡的私有Files目录下保存文件
103      public static boolean saveFileToSDCardPrivateFilesDir(byte[] data, String type, String fileName, Context context) {
104          BufferedOutputStream bos = null;
105          if (isSDCardMounted()) {
106                File file = context.getExternalFilesDir(type);
107                try {
108                       bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
109                       bos.write(data);
110                       bos.flush();
111                       return true;
112                } catch (Exception e) {
113                       e.printStackTrace();
114                } finally {
115                       try {
116                             bos.close();
117                       } catch (IOException e) {
118                             // TODO Auto-generated catch block
119                             e.printStackTrace();
120                       }
121                }
122           }
123           return false;
124      }
125  
126      // 往SD卡的私有Cache目录下保存文件
127      public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String fileName, Context context) {
128           BufferedOutputStream bos = null;
129           if (isSDCardMounted()) {
130                 File file = context.getExternalCacheDir();
131                 try {
132                       bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
133                       bos.write(data);
134                       bos.flush();
135                       return true;
136                 } catch (Exception e) {
137                       e.printStackTrace();
138                 } finally {
139                       try {
140                             bos.close();
141                       } catch (IOException e) {
142                             // TODO Auto-generated catch block
143                             e.printStackTrace();
144                       }
145                }
146           }
147           return false;
148      }
149  
150      // 保存bitmap图片到SDCard的私有Cache目录
151      public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap, String fileName, Context context) {
152           if (isSDCardMounted()) {
153                 BufferedOutputStream bos = null;
154                 // 获取私有的Cache缓存目录
155                 File file = context.getExternalCacheDir();
156  
157                 try {
158                        bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
159                        if (fileName != null && (fileName.contains(".png") || fileName.contains(".PNG"))) {
160                               bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
161                        } else {
162                               bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
163                        }
164                        bos.flush();
165                 } catch (Exception e) {
166                        e.printStackTrace();
167                 } finally {
168                        if (bos != null) {
169                             try {
170                                  bos.close();
171                             } catch (IOException e) {
172                                  e.printStackTrace();
173                             }
174                        }
175                  }
176                  return true;
177           } else {
178                 return false;
179           }
180      }
181  
182      // 从SD卡获取文件
183      public static byte[] loadFileFromSDCard(String fileDir) {
184           BufferedInputStream bis = null;
185           ByteArrayOutputStream baos = new ByteArrayOutputStream();
186  
187           try {
188                 bis = new BufferedInputStream(new FileInputStream(new File(fileDir)));
189                 byte[] buffer = new byte[8 * 1024];
190                 int c = 0;
191                 while ((c = bis.read(buffer)) != -1) {
192                      baos.write(buffer, 0, c);
193                      baos.flush();
194                 }
195                 return baos.toByteArray();
196           } catch (Exception e) {
197                 e.printStackTrace();
198           } finally {
199                 try {
200                      baos.close();
201                      bis.close();
202                 } catch (IOException e) {
203                      e.printStackTrace();
204                 }
205           }
206           return null;
207      }
208  
209      // 从SDCard中寻找指定目录下的文件,返回Bitmap
210      public Bitmap loadBitmapFromSDCard(String filePath) {
211           byte[] data = loadFileFromSDCard(filePath);
212           if (data != null) {
213                Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
214                if (bm != null) {
215                      return bm;
216                }
217           }
218           return null;
219      }
220  
221      // 获取SD卡公有目录的路径
222      public static String getSDCardPublicDir(String type) {
223           return Environment.getExternalStoragePublicDirectory(type).toString();
224      }
225  
226      // 获取SD卡私有Cache目录的路径
227      public static String getSDCardPrivateCacheDir(Context context) {
228           return context.getExternalCacheDir().getAbsolutePath();
229      }
230  
231      // 获取SD卡私有Files目录的路径
232      public static String getSDCardPrivateFilesDir(Context context, String type) {
233           return context.getExternalFilesDir(type).getAbsolutePath();
234      }
235  
236      public static boolean isFileExist(String filePath) {
237           File file = new File(filePath);
238           return file.isFile();
239      }
240  
241      // 从sdcard中删除文件
242      public static boolean removeFileFromSDCard(String filePath) {
243           File file = new File(filePath);
244           if (file.exists()) {
245                try {
246                      file.delete();
247                      return true;
248                } catch (Exception e) {
249                      return false;
250                }
251           } else {
252                return false;
253           }
254      }
255 }

 

posted on 2019-09-11 17:41  Reverse-xiaoyu  阅读(642)  评论(0编辑  收藏  举报