Back to project page CommonLibs.
The source code is released under:
Apache License
If you think the Android project CommonLibs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.alex.common.utils; /*from w w w. j av a2s .c o m*/ import java.io.File; import java.util.Calendar; import java.util.GregorianCalendar; import com.alex.common.AppConfig; import android.os.Environment; import android.text.TextUtils; /** * ??????? * @author caisenchuan */ public class FileUtils { /*-------------------------- * ??? *-------------------------*/ private static final String TAG = FileUtils.class.getSimpleName(); /*-------------------------- * ????? *-------------------------*/ /** * ?????? */ public enum PathType { /**photo??*/ PHOTO, /**download??*/ DOWNLOAD, /**????????*/ SHARE } /*-------------------------- * ???????? *-------------------------*/ /*-------------------------- * public?? *-------------------------*/ /** * ?????????????? */ public static void deleteAllFiles(String path) { if(!TextUtils.isEmpty(path)) { File f = new File(path); deleteAllFiles(f); } } /** * ????????????? * @param root */ public static void deleteAllFiles(File root) { File files[] = root.listFiles(); if (files != null) { for (File f : files) { if (f.isDirectory()) { // ?????????? deleteAllFiles(f); try { f.delete(); } catch (Exception e) { KLog.e(TAG, "Exception", e); } } else { if (f.exists()) { // ???????? deleteAllFiles(f); try { f.delete(); } catch (Exception e) { KLog.e(TAG, "Exception", e); } } } } } } /** * ?????? */ public static void deleteFile(String picPath) { if(picPath != null) { File file = new File(picPath); if(file != null && file.exists()) { file.delete(); } picPath = null; } } /** * ??SD????????? */ public static boolean isSDMount() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } /** * ???????? * @param type ?????? */ public static String getDir(PathType type) { String dir = ""; if(type == PathType.PHOTO) { dir = AppConfig.getDirPhoto(); } else if(type == PathType.DOWNLOAD) { dir = AppConfig.getDirDownload(); } else if(type == PathType.SHARE) { dir = AppConfig.getDirShare(); } else { dir = AppConfig.getDirApp(); } File sd = Environment.getExternalStorageDirectory(); File file = new File(sd.getPath() + dir); if (!file.exists()) { file.mkdirs(); } return file.getAbsolutePath(); } /** * ??????????????? ?? + ??????? ???????? * @param type ?????? * @param ext ????????? */ public static String getUniqPath(PathType type, String ext) { String picDir = getDir(type); GregorianCalendar calendar = new GregorianCalendar(); String date_s = String.format("%d%02d%02d_%02d%02d%02d", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); String filename = String.format("%s/%s%s", picDir, date_s, ext); File file = new File(filename); int i = 1; // ??????????????? while (file.exists() && i < 10000) { filename = String.format("%s/%s(%s)%s", picDir, date_s, i, ext); file = new File(filename); i++; } KLog.d(TAG, "filename : " + filename); return filename; } /*-------------------------- * protected??packet?? *-------------------------*/ /*-------------------------- * private?? *-------------------------*/ }