1 package com.zyh.sdcardHelper; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.ByteArrayOutputStream; 6 import java.io.File; 7 import java.io.FileInputStream; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 11 import android.content.Context; 12 import android.graphics.Bitmap; 13 import android.graphics.BitmapFactory; 14 import android.os.Environment; 15 import android.os.StatFs; 16 import android.text.format.Formatter; 17 18 public class SDCardHelper { 19 //判断SD卡是否被挂载 20 public static boolean isSDCardMounted(){ 21 return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 22 } 23 24 //获取SDCard的绝对路径目录 25 public static String getSDCardBaseDir(){ 26 if(isSDCardMounted()){ 27 return Environment.getExternalStorageDirectory().getAbsolutePath(); 28 } 29 return null; 30 } 31 32 //获取SDCard总大小 33 public static String getSDCardTotalSize(Context context){ 34 if(isSDCardMounted()){ 35 StatFs fs = new StatFs(getSDCardBaseDir()); 36 long blockSize = fs.getBlockSize(); 37 long totalBlocks = fs.getBlockCount(); 38 long totalSize = blockSize * totalBlocks; 39 return Formatter.formatFileSize(context, totalSize); 40 } 41 return null; 42 } 43 44 //获取sd卡的剩余空间 45 public static String getSDCardFreeSize(Context context){ 46 if(isSDCardMounted()){ 47 StatFs fs = new StatFs(getSDCardBaseDir()); 48 long blockSize = fs.getBlockSize(); 49 long freeBlocks = fs.getFreeBlocks(); 50 long freeSize = blockSize * freeBlocks; 51 return Formatter.formatFileSize(context, freeSize); 52 } 53 return null; 54 } 55 56 //获取sd卡的可用空间 57 public static String getSDCardAvailableSize(Context context){ 58 if(isSDCardMounted()){ 59 StatFs fs = new StatFs(getSDCardBaseDir()); 60 long blockSize = fs.getBlockSize(); 61 long availableBlocks = fs.getFreeBlocks(); 62 long availableSize = blockSize * availableBlocks; 63 return Formatter.formatFileSize(context, availableSize); 64 } 65 return null; 66 } 67 68 //往sd卡的公有目录下保存数据进文件 69 public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String fileName){ 70 BufferedOutputStream bos = null; 71 if(isSDCardMounted()){ 72 try { 73 File file = Environment.getExternalStoragePublicDirectory(type); 74 bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName))); 75 bos.write(data); 76 bos.flush(); 77 return true; 78 } catch (Exception e) { 79 // TODO Auto-generated catch block 80 e.printStackTrace(); 81 }finally{ 82 try { 83 bos.close(); 84 } catch (IOException e) { 85 // TODO Auto-generated catch block 86 e.printStackTrace(); 87 } 88 } 89 } 90 return false; 91 } 92 93 //往sd卡用户自定义目录中保存数据进文件 94 public static boolean savaFileToSDCardCustomDir(byte[] data, String dir, String fileName){ 95 BufferedOutputStream bos = null; 96 if(isSDCardMounted()){ 97 try { 98 File file = new File(getSDCardBaseDir() + File.separator + dir); 99 if(!file.exists()){ 100 file.mkdirs();//递归创建自定义目录 101 } 102 bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName))); 103 bos.write(data); 104 bos.flush(); 105 return true; 106 } catch (Exception e) { 107 // TODO Auto-generated catch block 108 e.printStackTrace(); 109 }finally{ 110 try { 111 bos.close(); 112 } catch (IOException e) { 113 // TODO Auto-generated catch block 114 e.printStackTrace(); 115 } 116 } 117 } 118 return false; 119 } 120 121 //往sd卡私有Files目录下保存数据进文件 122 //注意如果不加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>这个权限,则报空指针异常 123 //type可用为null 124 public static boolean saveFileToSDCardPrivateFilesDir(byte[] data, String type, String fileName, Context context){ 125 BufferedOutputStream bos = null; 126 if(isSDCardMounted()){ 127 try { 128 File file = context.getExternalFilesDir(type); 129 bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName))); 130 bos.write(data); 131 bos.flush(); 132 return true; 133 } catch (Exception e) { 134 // TODO Auto-generated catch block 135 e.printStackTrace(); 136 }finally{ 137 try { 138 bos.close(); 139 } catch (IOException e) { 140 // TODO Auto-generated catch block 141 e.printStackTrace(); 142 } 143 } 144 } 145 return false; 146 } 147 148 //往sd卡的私有Cache目录下保存数据进文件 149 //cache目录和files目录是同一级的 150 public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String fileName, Context context){ 151 BufferedOutputStream bos = null; 152 if(isSDCardMounted()){ 153 try { 154 File file = context.getExternalCacheDir(); 155 bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName))); 156 bos.write(data); 157 bos.flush(); 158 return true; 159 } catch (Exception e) { 160 // TODO Auto-generated catch block 161 e.printStackTrace(); 162 }finally{ 163 try { 164 bos.close(); 165 } catch (IOException e) { 166 // TODO Auto-generated catch block 167 e.printStackTrace(); 168 } 169 } 170 } 171 return false; 172 } 173 174 //把bitmap保存在sd卡的私有cache目录中 175 public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap,String fileName, Context context){ 176 if(isSDCardMounted()){ 177 BufferedOutputStream bos = null; 178 File file = context.getExternalCacheDir(); 179 try { 180 bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName))); 181 //100表示压缩率为0 182 if(fileName != null && (fileName.endsWith(".png") || fileName.endsWith(".PNG"))){ 183 bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); 184 }else{ 185 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); 186 } 187 bos.flush(); 188 } catch (Exception e) { 189 // TODO Auto-generated catch block 190 e.printStackTrace(); 191 }finally{ 192 if(bos != null){ 193 try { 194 bos.close(); 195 } catch (IOException e) { 196 // TODO Auto-generated catch block 197 e.printStackTrace(); 198 } 199 } 200 } 201 return true; 202 } 203 return false; 204 } 205 206 //从sd卡的路径中获取文件的内容 207 public static byte[] readFileFromSDCard(String filePath){ 208 BufferedInputStream bis = null; 209 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 210 try { 211 bis = new BufferedInputStream(new FileInputStream(new File(filePath))); 212 byte[] buffer = new byte[8 * 1024]; 213 int hasRead = 0; 214 while((hasRead = bis.read(buffer)) != -1){ 215 baos.write(buffer, 0, hasRead); 216 baos.flush(); 217 } 218 return baos.toByteArray(); 219 } catch (Exception e) { 220 // TODO Auto-generated catch block 221 e.printStackTrace(); 222 }finally{ 223 try { 224 baos.close(); 225 bis.close(); 226 } catch (IOException e) { 227 // TODO Auto-generated catch block 228 e.printStackTrace(); 229 } 230 } 231 return null; 232 } 233 234 //从sd卡中获取Bitmap文件 235 public static Bitmap loadBitmapFromSDCard(String filePath){ 236 byte[] data = readFileFromSDCard(filePath); 237 if(data != null){ 238 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 239 if(bitmap != null){ 240 return bitmap; 241 } 242 } 243 return null; 244 } 245 246 //获取sd卡的公有目录路径 247 public static String getSDCardPublicDir(String type){ 248 return Environment.getExternalStoragePublicDirectory(type).toString(); 249 250 } 251 252 //获取sd卡私有cache目录路径 253 public static String getSDCardPrivateCacheDir(Context context){ 254 return context.getExternalCacheDir().getAbsolutePath(); 255 } 256 257 //sd卡私有files目录下的路径 258 public static String getSDCardPrivateFilesDir(Context context, String type){ 259 return context.getExternalFilesDir(type).getAbsolutePath(); 260 } 261 262 //从sd卡中删除文件 263 public static boolean removeFileFromSDCard(String filePath){ 264 File file = new File(filePath); 265 if(file.exists()){ 266 try { 267 file.delete(); 268 return true; 269 } catch (Exception e) { 270 return false; 271 } 272 } 273 return false; 274 } 275 276 }
参考:http://www.androidchina.net/4106.html
时间: 2024-10-11 14:49:27