SDcard

Posted on 2016-04-01 17:40  邱小贱  阅读(360)  评论(0)    收藏  举报
  1 import java.io.ByteArrayOutputStream;
  2 import java.io.File;
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.io.OutputStream;
  9 import android.content.Context;
 10 import android.graphics.Bitmap;
 11 import android.graphics.BitmapFactory;
 12 import android.os.Environment;
 13 import android.os.StatFs;
 14 
 15 public class SDCardUtils {
 16 
 17     // 判断SDCard是否挂载
 18     public static boolean isSDCardMounted() {
 19         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
 20             return true;
 21         }
 22         return false;
 23     }
 24 
 25     // 获得SDCard的根目录 如/storage/sdcard/
 26     public static String getSDCardBaseDir() {
 27         if (isSDCardMounted()) {
 28             File dir = Environment.getExternalStorageDirectory();
 29             return dir.getAbsolutePath();
 30         }
 31         return null;
 32     }
 33 
 34     // 获得SD卡的全部空间大小(单位:M)
 35     public static long getSDCardSize() {
 36         if (isSDCardMounted()) {
 37             String dir = getSDCardBaseDir();
 38             StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
 39             long blockCount = statFs.getBlockCountLong(); // 有多少块
 40             long blockSize = statFs.getBlockSizeLong(); // 每块有多大
 41             return blockCount * blockSize / 1024 / 1024;
 42         }
 43         return 0;
 44     }
 45 
 46     // 获取SDCard空闲空间的大小(单位:M) (有多少空间还没被占用的)
 47     public static long getSDCardFreeSize() {
 48         if (isSDCardMounted()) {
 49             String dir = getSDCardBaseDir();
 50             StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
 51             long freeBlockCount = statFs.getFreeBlocksLong(); // 空闲的块
 52             long blockSize = statFs.getBlockSizeLong(); // 每块有多大
 53             return freeBlockCount * blockSize / 1024 / 1024;
 54         }
 55         return 0;
 56     }
 57 
 58     // 获取SDCard可用空间的大小(单位:M) (有多少空间你可以使用的)
 59     public static long getSDCardAvailSize() {
 60         if (isSDCardMounted()) {
 61             String dir = getSDCardBaseDir();
 62             StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
 63             long availBlockCount = statFs.getAvailableBlocksLong(); // 可用的块
 64             long blockSize = statFs.getBlockSizeLong(); // 每块有多大
 65             return availBlockCount * blockSize / 1024 / 1024;
 66         }
 67         return 0;
 68     }
 69 
 70     // 往SDCard公有目录下保存文件 (九大公有目录中的一个,具体由type指定) /storage/sdcard/{type}
 71     public static boolean saveData2SDCardPublicDir(byte[] data, String type, String filename) {
 72         if (isSDCardMounted()) {
 73             // 文件名:/storage/sdcard/Musics/111.txt
 74             // 文件名:getSDCardBaseDir()/{type}/{filename}
 75             String baseDir = getSDCardBaseDir();
 76             String file = baseDir + File.separator + type + "/" + filename;
 77             try {
 78                 OutputStream os = new FileOutputStream(file);
 79                 os.write(data);
 80                 os.close();
 81                 return true;
 82             } catch (FileNotFoundException e) {
 83                 // TODO Auto-generated catch block
 84                 e.printStackTrace();
 85             } catch (IOException e) {
 86                 // TODO Auto-generated catch block
 87                 e.printStackTrace();
 88             }
 89         }
 90         return false;
 91     }
 92 
 93     // 往SDCard的自定义目录中保存数据 /storage/sdcard/{dir}
 94     public static boolean saveData2SDCardCustomDir(byte[] data, String dir, String filename) {
 95         if (isSDCardMounted()) {
 96             // /storage/sdcard/{dir}
 97             // getSDCardBaseDir()/{dir}/{filename}
 98             String baseDir = getSDCardBaseDir(); // SDCard目录
 99             String path = baseDir + "/" + dir; // SDCard中自定义的目录
100             // 如果path不存在的话 需要创建目录
101             File customPath = new File(path);
102             if (!customPath.exists()) {
103                 customPath.mkdir();
104             }
105             String file = path + "/" + filename; // SDCard中自定义目录中的文件
106             try {
107                 OutputStream os = new FileOutputStream(file);
108                 os.write(data);
109                 os.close();
110                 return true;
111             } catch (FileNotFoundException e) {
112                 // TODO Auto-generated catch block
113                 e.printStackTrace();
114             } catch (IOException e) {
115                 // TODO Auto-generated catch block
116                 e.printStackTrace();
117             }
118 
119         }
120         return false;
121     }
122 
123     // :往SDCard的私有File目录下保存文件
124     // /storage/sdcard/Android/data/包名/files/{type}/{filename}
125     public static boolean saveData2SDCardPrivateFileDir(byte[] data, String type, String filename, Context context) {
126         if (isSDCardMounted()) {
127             // storage/sdcard/Android/data/包名/files/{type}/{filename}
128             File path = context.getExternalFilesDir(type);
129             if (!path.exists()) {
130                 path.mkdir();
131             }
132             String file = path.getAbsolutePath() + "/" + filename; // 文件路径
133             try {
134                 OutputStream os = new FileOutputStream(file);
135                 os.write(data);
136                 os.close();
137                 return true;
138             } catch (FileNotFoundException e) {
139                 // TODO Auto-generated catch block
140                 e.printStackTrace();
141             } catch (IOException e) {
142                 // TODO Auto-generated catch block
143                 e.printStackTrace();
144             }
145         }
146         return false;
147     }
148 
149     // 往SDCard的私有Cache目录下保存文件 /storage/sdcard/Android/data/包名/cache/{filename}
150     public static boolean saveData2SDCardPrivateCacheDir(byte[] data, String filename, Context context) {
151         if (isSDCardMounted()) {
152             // storage/sdcard/Android/data/包名/cache/{filename}
153             File path = context.getExternalCacheDir();
154             if (!path.exists()) {
155                 path.mkdir();
156             }
157             String file = path.getAbsolutePath() + "/" + filename;
158             try {
159                 OutputStream os = new FileOutputStream(file);
160                 os.write(data);
161                 os.close();
162                 return true;
163             } catch (FileNotFoundException e) {
164                 // TODO Auto-generated catch block
165                 e.printStackTrace();
166             } catch (IOException e) {
167                 // TODO Auto-generated catch block
168                 e.printStackTrace();
169             }
170         }
171         return false;
172     }
173 
174     // 往SDCard的私有Cache目录下保存图像 /storage/sdcard/Android/data/包名/cache/{filename}  xxx.png xxx.jpg
175     public static boolean saveBitmap2SDCardPrivateCacheDir(Bitmap bitmap, 
176             String filename, Context context) {
177         if(isSDCardMounted()) {
178             // 路径/storage/sdcard/Android/data/包名/cache/{filename}
179             File path = context.getExternalCacheDir();
180             if(!path.exists()) {
181                 path.mkdir();
182             }
183             String file = path.getAbsolutePath() + "/" + filename; // bmp、jpg
184             try {
185                 OutputStream os = new FileOutputStream(file);
186                 // 图片转换成byte[] 下面的代码代替os.write(...);
187                 // 判断到底是哪种图片的格式(jpg、png)
188                 if(filename.endsWith(".jpg") || filename.endsWith(".JPG")) {
189                     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); 
190                 } else if (filename.endsWith(".png") || filename.endsWith(".PNG")) {
191                     bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); 
192                 }
193                 os.close();
194                 return true;
195             } catch (FileNotFoundException e) {
196                 // TODO Auto-generated catch block
197                 e.printStackTrace();
198             } catch (IOException e) {
199                 // TODO Auto-generated catch block
200                 e.printStackTrace();
201             }
202         }
203         
204         return false;
205     }
206     
207     // 从SDCard读取指定文件 /storage/sdcard/{filePath}
208     public static byte[] loadFileFromSDCard(String filePath) {
209         if(isSDCardMounted()) {
210             // /storage/sdcard/{filePath}
211             // getSDCardBaseDir()/{filePath}
212             String path = getSDCardBaseDir();
213             String file = path + "/" + filePath;
214             try {
215                 InputStream is = new FileInputStream(file);
216                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
217                 byte[] buffer = new byte[1024];
218                 int ret;
219                 while(true) {
220                     ret = is.read(buffer);
221                     if(ret < 0) {
222                         break;
223                     }
224                     baos.write(buffer, 0, ret);
225                 }
226                 return baos.toByteArray(); // 返回字节数组
227             } catch (FileNotFoundException e) {
228                 // TODO Auto-generated catch block
229                 e.printStackTrace();
230             } catch (IOException e) {
231                 // TODO Auto-generated catch block
232                 e.printStackTrace();
233             }
234         }
235         return null;
236     }
237 
238     // 从SDCard读取Bitmap并返回 /storage/sdcard/{filePath}
239     public static Bitmap loadBitmapFromSDCard(String filePath) {
240         if(isSDCardMounted()) {
241             // 由于传入的参数已经是全路径了 因此不需要加上getSDCardBaseDir()
242             try {
243                 InputStream is = new FileInputStream(filePath);
244                 byte[] buffer = new byte[1024];
245                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
246                 int ret;
247                 while(true) {
248                     ret = is.read(buffer);
249                     if(ret < 0) {
250                         break;
251                     }
252                     baos.write(buffer, 0, ret);
253                 }
254                 byte[] data = baos.toByteArray();
255                 return BitmapFactory.decodeByteArray(data, 0, data.length);
256             } catch (FileNotFoundException e) {
257                 // TODO Auto-generated catch block
258                 e.printStackTrace();
259             } catch (IOException e) {
260                 // TODO Auto-generated catch block
261                 e.printStackTrace();
262             }
263         }
264         return null;
265     }
266 }

 

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3