Android中操作 SDCard文件

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

 

posted @ 2022-08-24 13:34  明天星期五啦  阅读(81)  评论(0编辑  收藏  举报