|
| 1 | +package com.blankj.utilcode.utils; |
| 2 | + |
| 3 | +import android.os.Environment; |
| 4 | +import android.os.StatFs; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileOutputStream; |
| 9 | + |
| 10 | +/** |
| 11 | + * <pre> |
| 12 | + * author: Blankj |
| 13 | + * blog : http://blankj.com |
| 14 | + * time : 2016/8/11 |
| 15 | + * desc : SD卡相关的工具类 |
| 16 | + * </pre> |
| 17 | + */ |
| 18 | +public class SDCardUtils { |
| 19 | + |
| 20 | + private SDCardUtils() { |
| 21 | + throw new UnsupportedOperationException("u can't fuck me..."); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * 获取设备SD卡是否可用 |
| 26 | + * |
| 27 | + * @return true : 可用<br>false : 不可用 |
| 28 | + */ |
| 29 | + public static boolean isSDCardEnable() { |
| 30 | + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * 获取设备SD卡路径 |
| 35 | + * <p>一般是/storage/emulated/0/</p> |
| 36 | + * |
| 37 | + * @return SD卡路径 |
| 38 | + */ |
| 39 | + public static String getSDCardPath() { |
| 40 | + return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * 获取SD卡的剩余容量 单位byte |
| 45 | + * |
| 46 | + * @return |
| 47 | + */ |
| 48 | + public static long getSDCardAllSize() { |
| 49 | + if (isSDCardEnable()) { |
| 50 | + StatFs stat = new StatFs(getSDCardPath()); |
| 51 | + // 获取空闲的数据块的数量 |
| 52 | + long availableBlocks = (long) stat.getAvailableBlocks() - 4; |
| 53 | + // 获取单个数据块的大小(byte) |
| 54 | + long freeBlocks = stat.getAvailableBlocks(); |
| 55 | + return freeBlocks * availableBlocks; |
| 56 | + } |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * 获取指定路径所在空间的剩余可用容量字节数,单位byte |
| 62 | + * |
| 63 | + * @param filePath |
| 64 | + * @return 容量字节 SDCard可用空间,内部存储可用空间 |
| 65 | + */ |
| 66 | + public static long getFreeBytes(String filePath) { |
| 67 | + // 如果是sd卡的下的路径,则获取sd卡可用容量 |
| 68 | + if (filePath.startsWith(getSDCardPath())) { |
| 69 | + filePath = getSDCardPath(); |
| 70 | + } else {// 如果是内部存储的路径,则获取内存存储的可用容量 |
| 71 | + filePath = Environment.getDataDirectory().getAbsolutePath(); |
| 72 | + } |
| 73 | + StatFs stat = new StatFs(filePath); |
| 74 | + long availableBlocks = (long) stat.getAvailableBlocks() - 4; |
| 75 | + return stat.getBlockSize() * availableBlocks; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * 获取系统存储路径 |
| 80 | + * |
| 81 | + * @return |
| 82 | + */ |
| 83 | + public static String getRootDirectoryPath() { |
| 84 | + return Environment.getRootDirectory().getAbsolutePath(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Check if the file is exists |
| 89 | + * |
| 90 | + * @param filePath |
| 91 | + * @param fileName |
| 92 | + * @return |
| 93 | + */ |
| 94 | + public static boolean isFileExistsInSDCard(String filePath, String fileName) { |
| 95 | + boolean flag = false; |
| 96 | + if (isSDCardEnable()) { |
| 97 | + File file = new File(filePath, fileName); |
| 98 | + if (file.exists()) { |
| 99 | + flag = true; |
| 100 | + } |
| 101 | + } |
| 102 | + return flag; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Write file to SD card |
| 107 | + * @param filePath |
| 108 | + * @param filename |
| 109 | + * @param content |
| 110 | + * @return |
| 111 | + * @throws Exception |
| 112 | + */ |
| 113 | + public static boolean saveFileToSDCard(String filePath, String filename, String content) |
| 114 | + throws Exception { |
| 115 | + boolean flag = false; |
| 116 | + if (isSDCardEnable()) { |
| 117 | + File dir = new File(filePath); |
| 118 | + if (!dir.exists()) { |
| 119 | + dir.mkdir(); |
| 120 | + } |
| 121 | + File file = new File(filePath, filename); |
| 122 | + FileOutputStream outStream = new FileOutputStream(file); |
| 123 | + outStream.write(content.getBytes()); |
| 124 | + outStream.close(); |
| 125 | + flag = true; |
| 126 | + } |
| 127 | + return flag; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Read file as stream from SD card |
| 132 | + * |
| 133 | + * @param fileName |
| 134 | + * String PATH = |
| 135 | + * Environment.getExternalStorageDirectory().getAbsolutePath() + |
| 136 | + * "/dirName"; |
| 137 | + * @return |
| 138 | + */ |
| 139 | + public static byte[] readFileFromSDCard(String filePath, String fileName) { |
| 140 | + byte[] buffer = null; |
| 141 | + try { |
| 142 | + if (isSDCardEnable()) { |
| 143 | + String filePaht = filePath + "/" + fileName; |
| 144 | + FileInputStream fin = new FileInputStream(filePaht); |
| 145 | + int length = fin.available(); |
| 146 | + buffer = new byte[length]; |
| 147 | + fin.read(buffer); |
| 148 | + fin.close(); |
| 149 | + } |
| 150 | + } catch (Exception e) { |
| 151 | + e.printStackTrace(); |
| 152 | + } |
| 153 | + return buffer; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Delete file |
| 158 | + * |
| 159 | + * @param filePath |
| 160 | + * @param fileName |
| 161 | + * filePath = |
| 162 | + * android.os.Environment.getExternalStorageDirectory().getPath() |
| 163 | + * @return |
| 164 | + */ |
| 165 | + public static boolean deleteSDFile(String filePath, String fileName) { |
| 166 | + File file = new File(filePath + "/" + fileName); |
| 167 | + return !(!file.exists() || file.isDirectory()) && file.delete(); |
| 168 | + } |
| 169 | +} |
0 commit comments