Android本地文件管理類:java
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; public class FileUtil { private static final String TAG = "FileUtil"; private Context context; private String SRCPATH; private String THUMBPATH; private boolean hasSD = false; /** * 單例模式 */ public FileUtil(Context context) { this.context = context; // 判斷是否含有SD卡 hasSD = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); String rootPath = hasSD ? (Environment.getExternalStorageDirectory().getPath() + File.separator + "Android" + File.separator + "data" + File.separator + SwitchAppTool.APPLICATION_ID + File.separator + "cache" + File.separator) : (this.context.getFilesDir().getPath() + File.separator + "Android" + File.separator + "data" + File.separator + SwitchAppTool.APPLICATION_ID + File.separator + "cache" + File.separator); THUMBPATH = rootPath + "thumb" + File.separator; SRCPATH = rootPath + "src" + File.separator; File src = new File(SRCPATH), thumb = new File(THUMBPATH); if (!src.exists()) { src.mkdirs(); } Log.e(TAG, src.getPath()); if (!thumb.exists()) { thumb.mkdirs(); } Log.e(TAG, thumb.getPath()); } private static FileUtil instance; public static FileUtil getInstance(Context context) { if (instance == null) { instance = new FileUtil(context); } return instance; } /** * 寫文件到sdcard * * @param fileName * @param bm * @param isThumb * @return */ public boolean writeFileToSD(String fileName, Bitmap bm, boolean isThumb) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos);// png類型 byte[] datas = baos.toByteArray(); return writeFileToSD(fileName, datas, isThumb); } /** * 將文件寫入SD卡 */ public boolean writeFileToSD(String fileName, byte[] datas, boolean isThumb) { try { File file = new File((isThumb ? THUMBPATH : SRCPATH) + fileName); Log.e(TAG, "writeFileToSD[]:" + file.getPath()); if (!file.exists()) { file.createNewFile();// 建立文件 } FileOutputStream stream = new FileOutputStream(file); stream.write(datas); stream.close(); return true; } catch (Exception e) { Log.e(TAG, "writeFileToSD[]:Error" + e.getMessage()); } return false; } /** * 判斷sdcard有沒有文件或文件夾 * * @param fileName * @return */ public boolean existsSrc(String fileName) { boolean flag = new File(SRCPATH + fileName).exists(); Log.e(TAG, "existsSrc[]:" + SRCPATH + fileName + ";result:" + flag); return flag; } private static CustomProgress customProgress = null; public boolean clear(Context context) { customProgress = CustomProgress.show(context, "loading...", false, null); File srcDir = new File(SRCPATH); String[] children = srcDir.list(); for (int i = 0; i < children.length; i++) { if (!new File(srcDir, children[i]).delete()) { return false; } } File thumbDir = new File(THUMBPATH); children = thumbDir.list(); for (int i = 0; i < children.length; i++) { if (!new File(thumbDir, children[i]).delete()) { return false; } } customProgress.dismiss(); return true; } public boolean existsThumb(String fileName) { boolean flag = new File(THUMBPATH + fileName).exists(); Log.e(TAG, "existsThumb[]:" + THUMBPATH + fileName + ";result:" + flag); return flag; } /** * 讀取文件 */ private Bitmap readSDFile(String fileName, boolean isThumb) { String pathName = (isThumb ? THUMBPATH : SRCPATH) + fileName; Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 獲取這個圖片的寬和高,注意此處的bitmap爲null bitmap = BitmapFactory.decodeFile(pathName, options); options.inJustDecodeBounds = false; // 設爲 false // 從新讀入圖片,讀取縮放後的bitmap,注意此次要把options.inJustDecodeBounds 設爲 false bitmap = BitmapFactory.decodeFile(pathName, options); // 利用ThumbnailUtils來建立縮略圖,這裏要指定要縮放哪一個Bitmap對象 return bitmap; } public Bitmap readSrcFile(String fileName) { return readSDFile(fileName, false); } public Bitmap readThumbFile(String fileName) { return readSDFile(fileName, true); } public String getSRCPATH() { return SRCPATH; } public String getTHUMBPATH() { return THUMBPATH; } }