android 外部存储文件,Android内部存储与外部存储的文件操作类

1 public classSDCardHelper {2

3 //判断SD卡是否被挂载

4 public static booleanisSDCardMounted() {5 //return Environment.getExternalStorageState().equals("mounted");

6 returnEnvironment.getExternalStorageState().equals(7 Environment.MEDIA_MOUNTED);8 }9

10 //获取SD卡的根目录

11 public staticString getSDCardBaseDir() {12 if(isSDCardMounted()) {13 returnEnvironment.getExternalStorageDirectory().getAbsolutePath();14 }15 return null;16 }17

18 //获取SD卡的完整空间大小,返回MB

19 public static longgetSDCardSize() {20 if(isSDCardMounted()) {21 StatFs fs = newStatFs(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 longgetSDCardFreeSize() {31 if(isSDCardMounted()) {32 StatFs fs = newStatFs(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 longgetSDCardAvailableSize() {42 if(isSDCardMounted()) {43 StatFs fs = newStatFs(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(newFile(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(newFile(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(newFile(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(newFile(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 booleansaveBitmapToSDCardPrivateCacheDir(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(newFile(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 = newByteArrayOutputStream();186

187 try{188 bis = new BufferedInputStream(new FileInputStream(newFile(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 returnbaos.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 publicBitmap 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 returnbm;216 }217 }218 return null;219 }220

221 //获取SD卡公有目录的路径

222 public staticString getSDCardPublicDir(String type) {223 returnEnvironment.getExternalStoragePublicDirectory(type).toString();224 }225

226 //获取SD卡私有Cache目录的路径

227 public staticString getSDCardPrivateCacheDir(Context context) {228 returncontext.getExternalCacheDir().getAbsolutePath();229 }230

231 //获取SD卡私有Files目录的路径

232 public staticString getSDCardPrivateFilesDir(Context context, String type) {233 returncontext.getExternalFilesDir(type).getAbsolutePath();234 }235

236 public static booleanisFileExist(String filePath) {237 File file = newFile(filePath);238 returnfile.isFile();239 }240

241 //从sdcard中删除文件

242 public static booleanremoveFileFromSDCard(String filePath) {243 File file = newFile(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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值