|
| 1 | +/** |
| 2 | + * Copyright 2014 Zhenguo Jin (jinzhenguo1990@gmail.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.worthed.util; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileInputStream; |
| 20 | +import java.io.FileOutputStream; |
| 21 | + |
| 22 | +/** |
| 23 | + * SDcard操作工具类 |
| 24 | + * |
| 25 | + * @author jingle1267@163.com |
| 26 | + * |
| 27 | + */ |
| 28 | +public class SDCardUtils { |
| 29 | + /** |
| 30 | + * Check the SD card |
| 31 | + * |
| 32 | + * @return |
| 33 | + */ |
| 34 | + public static boolean checkSDCardAvailable() { |
| 35 | + return android.os.Environment.getExternalStorageState().equals( |
| 36 | + android.os.Environment.MEDIA_MOUNTED); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Check if the file is exists |
| 41 | + * |
| 42 | + * @param filePath |
| 43 | + * @param fileName |
| 44 | + * @return |
| 45 | + */ |
| 46 | + public static boolean isFileExistsInSDCard(String filePath, String fileName) { |
| 47 | + boolean flag = false; |
| 48 | + if (checkSDCardAvailable()) { |
| 49 | + File file = new File(filePath, fileName); |
| 50 | + if (file.exists()) { |
| 51 | + flag = true; |
| 52 | + } |
| 53 | + } |
| 54 | + return flag; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Write file to SD card |
| 59 | + * |
| 60 | + * @param filePath |
| 61 | + * @param filename |
| 62 | + * @param content |
| 63 | + * @return |
| 64 | + * @throws Exception |
| 65 | + */ |
| 66 | + public static boolean saveFileToSDCard(String filePath, String filename, |
| 67 | + String content) throws Exception { |
| 68 | + boolean flag = false; |
| 69 | + if (checkSDCardAvailable()) { |
| 70 | + File dir = new File(filePath); |
| 71 | + if (!dir.exists()) { |
| 72 | + dir.mkdir(); |
| 73 | + } |
| 74 | + File file = new File(filePath, filename); |
| 75 | + FileOutputStream outStream = new FileOutputStream(file); |
| 76 | + outStream.write(content.getBytes()); |
| 77 | + outStream.close(); |
| 78 | + flag = true; |
| 79 | + } |
| 80 | + return flag; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Read file as stream from SD card |
| 85 | + * |
| 86 | + * @param fileName |
| 87 | + * String PATH = |
| 88 | + * Environment.getExternalStorageDirectory().getAbsolutePath() + |
| 89 | + * "/dirName"; |
| 90 | + * @return |
| 91 | + */ |
| 92 | + public static byte[] readFileFromSDCard(String filePath, String fileName) { |
| 93 | + byte[] buffer = null; |
| 94 | + try { |
| 95 | + if (checkSDCardAvailable()) { |
| 96 | + String filePaht = filePath + "/" + fileName; |
| 97 | + FileInputStream fin = new FileInputStream(filePaht); |
| 98 | + int length = fin.available(); |
| 99 | + buffer = new byte[length]; |
| 100 | + fin.read(buffer); |
| 101 | + fin.close(); |
| 102 | + } |
| 103 | + } catch (Exception e) { |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + return buffer; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Delete file |
| 111 | + * |
| 112 | + * @param filePath |
| 113 | + * @param fileName |
| 114 | + * filePath = |
| 115 | + * android.os.Environment.getExternalStorageDirectory().getPath() |
| 116 | + * @return |
| 117 | + */ |
| 118 | + public static boolean deleteSDFile(String filePath, String fileName) { |
| 119 | + File file = new File(filePath + "/" + fileName); |
| 120 | + if (file == null || !file.exists() || file.isDirectory()) |
| 121 | + return false; |
| 122 | + return file.delete(); |
| 123 | + } |
| 124 | +} |
0 commit comments