android文件的删除,创建
¹¤¾ßÀࡪ¡ªÎļþ²Ù×÷.txtpublic class FileUtils {
/*
* ÅжÏÎļþÊÇ·ñ´æÔÚ
* filePathµÄ²ÎÊý¸ñʽ£º/storage/emulated/0/.ToDayNote/UserInfo/28017616_1459134625049.jpg
*/
public static boolean fileExists(String filePath) {
File file = new File(filePath);
return file.exists();
}
/*
* Îļþɾ³ý
*/
public static boolean fileDelete(String filePath) {
File file = new File(filePath);
if (file.exists() == false) {
return false;
}
return file.delete();
}
/**
* ´´½¨Îļþ¼Ð
*/
public static boolean fileMkdirs(String filePath) {
File file = new File(filePath);
return file.mkdirs();
}
/**
* Ö÷Ŀ¼µØÖ·¡£ÔÚSD¿¨ÖУ¬´´½¨Ò»¸öÏîÄ¿µÄ¸ùĿ¼£¬¸ùĿ¼µÄ°üÃûΪ ToDayNote
* »ñÈ¡ÏîÄ¿Ö÷Ŀ¼µÄµØÖ·
*/
public static String toRootPath() {
String dir;
if (checkSDcard()) {
dir = Environment.getExternalStorageDirectory().getPath();
} else {
dir = Environment.getDataDirectory().getPath();
}
return dir + "/.myAppPath";
}
/**
* ¼ì²âÊÇ·ñ´æÔÚSdcard
* @return ´æÔÚ·µ»Øtrue£¬²»´æÔÚ·µ»Øfalse
*/
public static boolean checkSDcard() {
boolean flag = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
return flag;
}
/**
* ÔÚÖ÷Ŀ¼Ï£¬ÔÙÌí¼ÓÒ»²ã×ÓĿ¼¡£»ñÈ¡×ÓĿ¼µÄÃû³Æ
*/
public static String toDayNoteResources() {
return toRootPath() + "/Resources";
}
/**
* »ñÈ¡SD¿¨Â·¾¶
*/
public static String getSDCardPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator;
}
}
×îºóÀ´Ò»·ÝÈ«Ò»µãµÄ
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Base64;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.TimeZone;
import timber.log.Timber;
/**
* Îļþ¹ÜÀí¹¤¾ßÀà
* @version 1.5
*/
@SuppressLint("SdCardPath")
public class FileUtils {
public static final int SIZETYPE_B = 1;
public static final int SIZETYPE_KB = 2;
public static final int SIZETYPE_MB = 3;
public static final int SIZETYPE_GB = 4;
//ÓÃÓÚ´¢´æ·ÖÏíµÄʱºòÏÔʾAppͼ±êµÄ·¾¶
public static final String APP_LOGO=FileUtils.toDayEditorCache()+"/sharelogo.jpg";
public static boolean fileCopy(String oldFilePath, String newFilePath)
throws IOException {
// Èç¹ûÔÎļþ²»´æÔÚ
if (fileExists(oldFilePath) == false) {
return false;
}
boolean oldIsImage = oldFilePath.toLowerCase().contains(".jpg") || oldFilePath.contains(".jpeg") || oldFilePath.contains(".png");
boolean newIsImage = newFilePath.toLowerCase().contains(".jpg") || newFilePath.contains(".jpeg") || newFilePath.contains(".png");
if(oldIsImage && newIsImage)
{
ImageCompressUtils.saveImage2SpecifiedQuality(oldFilePath,newFilePath,ToDayApplication.mScreeWidth,ToDayApplication.mScreeHeight);
}else
{
// »ñµÃÔÎļþÁ÷
FileInputStream inputStream = new FileInputStream(new File(oldFilePath));
byte[] data = new byte[1024];
// Êä³öÁ÷
FileOutputStream outputStream = new FileOutputStream(new File(
newFilePath));
// ¿ªÊ¼´¦ÀíÁ÷
/*while (inputStream.read(data) != -1) {
outputStream.write(data);
}*/
int count = 0;
while ((count = inputStream.read(data)) > 0) {
outputStream.write(data, 0, count);
}
inputStream.close();
outputStream.close();
}
return true;
}
/**
* checking file if exists
*
* @param filePath
* @return true or false
*/
public static boolean fileExists(String filePath) {
File file = new File(filePath);
return file.exists();
}
/**
* create file
*
* @param filePath
* @return true or false
* @throws IOException
*/
public static boolean fileCreate(String filePath) throws IOException {
File file = new File(filePath);
return file.createNewFile();
}
/**
* delete file
*
* @param filePath
* @return true or false
*/
public static boolean fileDelete(String filePath) {
File file = new File(filePath);
if (file.exists() == false) {
return false;
}
return file.delete();
}
/**
* ´´½¨Îļþ¼Ð
*
* @param filePath
* @return true or false
*/
public static boolean fileMkdirs(String filePath) {
File file = new File(filePath);
return file.mkdirs();
}
/**
* Ö÷Ŀ¼µØÖ·
*
* @return
*/
@SuppressLint("SdCardPath")
public static String toDayNote() {
String dir;
if (checkSDcard()) {
dir = Environment.getExternalStorageDirectory().getPath();
} else {
dir = Environment.getDataDirectory().getPath();
}
return dir + "/.ToDayNote";
}
/**
* ´æ·ÅÎı¾µØÖ·
*
* @return
*/
public static String toDayNoteNotes() {
return toDayNote() + "/Notes";
}
/**
* ´æ·Å×ÊÔ´µØÖ·
*
* @return
*/
@SuppressLint("SdCardPath")
public static String toDayNoteResources() {
return toDayNote() + "/Resources";
}
/**
* Óû§ÐÅÏ¢´æ·ÅĿ¼
*
* @return
*/
@SuppressLint("SdCardPath")
public static String toDayUserInfo() {
return toDayNote() + "/UserInfo";
}
/**
* splash±³¾°Í¼Æ¬Ëù´æ·ÅµÄ·¾¶
* @return
*/
@SuppressLint("SdCardPath")
public static String toDaySplashBgImg() {
return toDayNoteResources() + "/splashImage";
}
/**
* ´íÎóÈÕÖ¾ÐÅÏ¢´æ·ÅĿ¼
*
* @return
*/
@SuppressLint("SdCardPath")
public static String toDayLog() {
return toDayNote() + "/Log/";
}
/**
* ÊÖд»º´æĿ¼
*
* @return
*/
public static String toDayEditorCache() {
return toDayNote() + "/EditorCache";
}
/**
* ×Ô¶¯Éú³ÉÃû³Æ
*
* @return
*/
public static String getFileName(String suffix) {
User user = ToDayApplication.getInstance().getCheckLogin();
String fileName = "";
if (user != null) {
fileName = user.getAccount() + "_" + System.currentTimeMillis() + (suffix);
} else {
fileName = new Random().nextInt(100000000) + "_" + System.currentTimeMillis() + (suffix);
}
Timber.e("Éú³ÉµÄеÄÎļþÃû:" + fileName);
return fileName;
}
public static String getCacheFileImagePath(String suffix) {
return ToDayApplication.getInstance().getExternalCacheDir() + File.separator + getFileName(suffix);
}
public static String getResourceFileImagePath(String suffix) {
return toDayNoteResources() + File.separator + getFileName(suffix);
}
/**
* UriתFile
*
* @param uri
* @return
*/
public static File UriToFile(Uri uri, ContentResolver contentResolver) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = contentResolver.query(uri, proj, null, null, null);
cursor.moveToFirst();
String img_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
File file = new File(img_path);
return file;
}
public static File UriToFile(Uri uri, Context context) {
String[] projection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DATE_ADDED};
long id = ContentUris.parseId(uri);
String[] selectionArgs = {String.valueOf(id)};
Cursor cursor = MediaStore.Images.Media.query(context.getContentResolver(), uri, projection, "_id=?", selectionArgs, null);
cursor.moveToFirst();
String img_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
File file = new File(img_path);
return file;
}
/**
* ¼ì²âÊÇ·ñ´æÔÚSdcard
*
* @return ´æÔÚ·µ»Øtrue£¬²»´æÔÚ·µ»Øfalse
*/
public static boolean checkSDcard() {
boolean flag = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
return flag;
}
/**
* ͼƬ±£´æ
*
* @param saveFileUrl
* @param mBitmap
* @return
*/
public static boolean saveToSDCard(String saveFileUrl, Bitmap mBitmap) {
String fileUrl = saveFileUrl;
boolean flag = false;
try {
FileOutputStream fos = new FileOutputStream(new File(fileUrl));
flag = mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
return flag;
}
/**
* »ñÈ¡SD¿¨Â·¾¶
*
* @return
*/
public static String getSDCardPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator;
}
/**
* »ñÈ¡SD¿¨µÄÊ£ÓàÈÝÁ¿ µ¥Î»byte
*
* @return
*/
public static long getSDCardAllSize() {
if (checkSDcard()) {
StatFs stat = new StatFs(getSDCardPath());
// »ñÈ¡¿ÕÏеÄÊý¾Ý¿éµÄÊýÁ¿
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
// »ñÈ¡µ¥¸öÊý¾Ý¿éµÄ´óС£¨byte£©
long freeBlocks = stat.getAvailableBlocks();
return freeBlocks * availableBlocks;
}
return 0;
}
/**
* »ñÈ¡Ö¸¶¨Â·¾¶ËùÔÚ¿Õ¼äµÄÊ£Óà¿ÉÓÃÈÝÁ¿×Ö½ÚÊý£¬µ¥Î»byte
*
* @param filePath
* @return ÈÝÁ¿×Ö½Ú SDCard¿ÉÓÿռ䣬ÄÚ²¿´æ´¢¿ÉÓÿռä
*/
public static long getFreeBytes(String filePath) {
// Èç¹ûÊÇsd¿¨µÄϵķ¾¶£¬Ôò»ñÈ¡sd¿¨¿ÉÓÃÈÝÁ¿
if (filePath.startsWith(getSDCardPath())) {
filePath = getSDCardPath();
} else {// Èç¹ûÊÇÄÚ²¿´æ´¢µÄ·¾¶£¬Ôò»ñÈ¡ÄÚ´æ´æ´¢µÄ¿ÉÓÃÈÝÁ¿
filePath = Environment.getDataDirectory().getAbsolutePath();
}
StatFs stat = new StatFs(filePath);
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
return stat.getBlockSize() * availableBlocks;
}
/**
* »ñȡϵͳ´æ´¢Â·¾¶
*
* @return
*/
public static String getRootDirectoryPath() {
return Environment.getRootDirectory().getAbsolutePath();
}
/**
* »ñÈ¡Îļþ´óС
*
* @param filePath
* @return
*/
public static long getFileSize(String filePath) {
long size = 0;
File file = new File(filePath);
if (file != null && file.exists()) {
size = file.length();
}
return size;
}
public static double getFileOrFilesSize(String filePath, int sizeType) {
File file = new File(filePath);
long blockSize = 0;
try {
if (file.isDirectory()) {
blockSize = getFileSizes(file);
} else {
blockSize = getFileSize(file);
}
} catch (Exception e) {
e.printStackTrace();
}
return FormetFileSize(blockSize, sizeType);
}
/**
* µ÷Óô˷½·¨×Ô¶¯¼ÆËãÖ¸¶¨Îļþ»òÖ¸¶¨Îļþ¼ÐµÄ´óС *
*
* @param filePath * Îļþ·¾¶
* @return ¼ÆËãºÃµÄ´øB¡¢KB¡¢MB¡¢GBµÄ×Ö·û´®
*/
public static String getAutoFileOrFilesSize(String filePath) {
File file = new File(filePath);
long blockSize = 0;
try {
if (file.isDirectory()) {
blockSize = getFileSizes(file);
} else {
blockSize = getFileSize(file);
}
} catch (Exception e) {
e.printStackTrace();
}
return FormetFileSize(blockSize);
}
/**
* »ñÈ¡Ö¸¶¨Îļþ´óС * * @param f * @return * @throws Exception
*/
private static long getFileSize(File file) throws Exception {
long size = 0;
if (file.exists()) {
FileInputStream fis = null;
fis = new FileInputStream(file);
size = fis.available();
} else {
file.createNewFile();
}
return size;
}
/**
* »ñÈ¡Ö¸¶¨Îļþ¼Ð * * @param f * @return * @throws Exception
*/
private static long getFileSizes(File f) throws Exception {
long size = 0;
File flist[] = f.listFiles();
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getFileSizes(flist[i]);
} else {
size = size + getFileSize(flist[i]);
}
}
return size;
}
/**
* ת»»Îļþ´óС * * @param fileS * @return
*/
private static String FormetFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
String wrongSize = "0B";
if (fileS == 0) {
return wrongSize;
}
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "MB";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "GB";
}
return fileSizeString;
}
/**
* ת»»Îļþ´óС,Ö¸¶¨×ª»»µÄÀàÐÍ * * @param fileS * @param sizeType * @return
*/
private static double FormetFileSize(long fileS, int sizeType) {
DecimalFormat df = new DecimalFormat("#.00");
double fileSizeLong = 0;
switch (sizeType) {
case SIZETYPE_B:
fileSizeLong = Double.valueOf(df.format((double) fileS));
break;
case SIZETYPE_KB:
fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
break;
case SIZETYPE_MB:
fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
break;
case SIZETYPE_GB:
fileSizeLong = Double.valueOf(df
.format((double) fileS / 1073741824));
break;
default:
break;
}
return fileSizeLong;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = MediaStore.MediaColumns._ID + "=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection,
selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri . This is useful for
* MediaStore Uris , and other file - based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri,
String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = MediaStore.MediaColumns.DATA;
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection,
selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri
.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri
.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
}
/*****************
* ÓïÒôÏà¹Ø
******************/
public static String getDecodeContent(Context context, String assetFileName) throws IOException {
AssetManager assetManager = context.getAssets();
InputStream stream = assetManager.open(assetFileName);
StringBuilder sb = new StringBuilder();
byte[] data = new byte[512];
// ¿ªÊ¼´¦ÀíÁ÷
int count = 0;
while ((count = stream.read(data)) > 0) {
sb.append(new String(data, 0, count));
}
byte[] decode = Base64.decode(sb.toString(), Base64.DEFAULT);
String str = new String(decode);
return str;
}
public static void write(String path, String content, String encoding) throws IOException {
File file = new File(path);
file.delete();
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
writer.write(content);
writer.close();
}
/**
* »ñÈ¡·þÎñ¶ËͼƬ´æ´¢Ïà¶Ô·¾¶
*
* @return
*/
public static String getServerImagePath() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
return year + "/" + month + "/" + day + "/" + "images/";
}
/**
* »ñÈ¡·þÎñ¶ËÓïÒô´æ´¢Ïà¶Ô·¾¶
*
* @return
*/
public static String getServerVoicePath() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
return year + "/" + month + "/" + day + "/" + "voices/";
}
}