package com.demo.app.utils; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; 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 com.demo.lib.common.util.common.StringUtils; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class FileUtils { /** * 写文本文�?在Android系统中,文件保存�?/data/data/PACKAGE_NAME/files 目录�? * * @param context */ public static void write(Context context, String fileName, String content) { if (content == null) content = ""; try { FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 读取文本文件 * * @param context * @param fileName * @return */ public static String read(Context context, String fileName) { try { FileInputStream in = context.openFileInput(fileName); return readInStream(in); } catch (Exception e) { e.printStackTrace(); } return ""; } private static String readInStream(FileInputStream inStream) { try { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[512]; int length = -1; while ((length = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, length); } outStream.close(); inStream.close(); return outStream.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } public static File createFile(String folderPath, String fileName) { File destDir = new File(folderPath); if (!destDir.exists()) { destDir.mkdirs(); } return new File(folderPath, fileName + fileName); } public static void copyFile(InputStream in, File file) { if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } if (file.exists()) { try { FileOutputStream os = new FileOutputStream(file); byte[] b = new byte[1024]; int len = -1; while ((len = in.read(b)) != -1) { os.write(b, 0, len); } in.close(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * 向手机写图片 * * @param buffer * @param folder * @param fileName * @return */ public static boolean writeFile(byte[] buffer, String folder, String fileName) { boolean writeSucc = false; boolean sdCardExist = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); String folderPath = ""; if (sdCardExist) { folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator; } else { writeSucc = false; } File fileDir = new File(folderPath); if (!fileDir.exists()) { fileDir.mkdirs(); } File file = new File(folderPath + fileName); FileOutputStream out = null; try { out = new FileOutputStream(file); out.write(buffer); writeSucc = true; } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } return writeSucc; } /** * 根据文件绝对路径获取文件名 * * @param filePath * @return */ public static String getFileName(String filePath) { if (StringUtils.isEmpty(filePath)) return ""; return filePath.substring(filePath.lastIndexOf(File.separator) + 1); } /** * 根据文件的绝对路径获取文件名但不包含扩展名 * * @param filePath * @return */ public static String getFileNameNoFormat(String filePath) { if (StringUtils.isEmpty(filePath)) { return ""; } int point = filePath.lastIndexOf('.'); return filePath.substring(filePath.lastIndexOf(File.separator) + 1, point); } /** * 获取文件扩展名 * * @param fileName * @return */ public static String getFileFormat(String fileName) { if (StringUtils.isEmpty(fileName)) return ""; int point = fileName.lastIndexOf('.'); return fileName.substring(point + 1); } /** * 获取文件大小 * * @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; } /** * 获取文件大小 * * @param size 字节 * @return */ public static String getFileSize(long size) { if (size <= 0) return "0"; java.text.DecimalFormat df = new java.text.DecimalFormat("##.##"); float temp = (float) size / 1024; if (temp >= 1024) { return df.format(temp / 1024) + "M"; } else { return df.format(temp) + "K"; } } /** * 转换文件大小 * * @param fileS * @return B/KB/MB/GB */ public static String formatFileSize(long fileS) { java.text.DecimalFormat df = new java.text.DecimalFormat("#.00"); String fileSizeString = ""; 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) + "G"; } return fileSizeString; } /** * 获取目录文件大小 * * @param dir * @return */ public static long getDirSize(File dir) { if (dir == null) { return 0; } if (!dir.isDirectory()) { return 0; } long dirSize = 0; File[] files = dir.listFiles(); for (File file : files) { if (file.isFile()) { dirSize += file.length(); } else if (file.isDirectory()) { dirSize += file.length(); dirSize += getDirSize(file); // 递归调用继续统计 } } return dirSize; } /** * 获取目录文件个数 * * @return */ public long getFileList(File dir) { long count = 0; File[] files = dir.listFiles(); count = files.length; for (File file : files) { if (file.isDirectory()) { count = count + getFileList(file);// 递归 count--; } } return count; } public static byte[] toBytes(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int ch; while ((ch = in.read()) != -1) { out.write(ch); } byte[] buffer = out.toByteArray(); out.close(); return buffer; } /** * �?��文件是否存在 * * @param name * @return */ public static boolean checkFileExists(String name) { boolean status; if (!name.equals("")) { File path = Environment.getExternalStorageDirectory(); File newPath = new File(path.toString() + name); status = newPath.exists(); } else { status = false; } return status; } public static boolean existFile(String path) { File file = new File(path); return file.isFile() && file.exists(); } /** * 计算SD卡的剩余空间 * * @return 返回-1,说明没有安装sd�? */ public static long getFreeDiskSpace() { String status = Environment.getExternalStorageState(); long freeSpace = 0; if (status.equals(Environment.MEDIA_MOUNTED)) { try { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); freeSpace = availableBlocks * blockSize / 1024; } catch (Exception e) { e.printStackTrace(); } } else { return -1; } return (freeSpace); } /** * 新建目录 * * @param directoryName * @return */ public static boolean createDirectory(String directoryName) { boolean status; if (!directoryName.equals("")) { File path = Environment.getExternalStorageDirectory(); File newPath = new File(path.toString() + directoryName); status = newPath.mkdir(); status = true; } else status = false; return status; } /** * �?��是否安装SD�? * * @return */ public static boolean checkSaveLocationExists() { String sDCardStatus = Environment.getExternalStorageState(); boolean status; status = sDCardStatus.equals(Environment.MEDIA_MOUNTED); return status; } /** * 删除目录(包括:目录里的所有文�? * * @param fileName * @return */ public static boolean deleteDirectory(String fileName) { boolean status; SecurityManager checker = new SecurityManager(); if (!fileName.equals("")) { File path = Environment.getExternalStorageDirectory(); File newPath = new File(path.toString() + fileName); checker.checkDelete(newPath.toString()); if (newPath.isDirectory()) { String[] listfile = newPath.list(); // delete all files within the specified directory and then // delete the directory try { for (int i = 0; i < listfile.length; i++) { File deletedFile = new File(newPath.toString() + "/" + listfile[i]); deletedFile.delete(); } newPath.delete(); status = true; } catch (Exception e) { e.printStackTrace(); status = false; } } else status = false; } else status = false; return status; } /** * 删除文件 * * @param fileName * @return */ public static boolean deleteFile(String fileName) { boolean status; SecurityManager checker = new SecurityManager(); if (!fileName.equals("")) { File path = Environment.getExternalStorageDirectory(); File newPath = new File(path.toString() + fileName); checker.checkDelete(newPath.toString()); if (newPath.isFile()) { try { newPath.delete(); status = true; } catch (SecurityException se) { se.printStackTrace(); status = false; } } else status = false; } else status = false; return status; } public static final String getTempFilePath(Context context) { return context.getCacheDir() + File.separator + "temp"; } public static String getSDPath() { File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); // 判断sd卡是否存�? if (sdCardExist) { sdDir = Environment.getExternalStorageDirectory();// 获取跟目�? } return sdDir.getPath(); } // 将图片保存到指定的路径 public static String savePic(String filePath, String name, Bitmap bitmap) { File file = new File(filePath); if (!file.exists()) file.mkdirs(); File f = new File(filePath + File.separator + name); try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } return f.getPath(); } // 将图片保存到指定的路径 public static String savePic(String filePath, Bitmap bitmap) { File f = new File(filePath); try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } return f.getPath(); } public static String getFilePathByUri(Context context, Uri uri) { String path = null; // 以 file:// 开头的 if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { path = uri.getPath(); return path; } // 以 content:// 开头的,比如 content://media/extenral/images/media/17766 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null); if (cursor != null) { if (cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); if (columnIndex > -1) { path = cursor.getString(columnIndex); } } cursor.close(); } return path; } // 4.4及之后的 是以 content:// 开头的,比如 content://com.android.providers.media.documents/document/image%3A235700 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (DocumentsContract.isDocumentUri(context, uri)) { if (isExternalStorageDocument(uri)) { // ExternalStorageProvider final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { path = Environment.getExternalStorageDirectory() + "/" + split[1]; return path; } } else if (isDownloadsDocument(uri)) { // DownloadsProvider final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); path = getDataColumn(context, contentUri, null, null); return path; } else if (isMediaDocument(uri)) { // MediaProvider 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 = "_id=?"; final String[] selectionArgs = new String[]{split[1]}; path = getDataColumn(context, contentUri, selection, selectionArgs); return path; } } } return null; } private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; } private static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } private static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } private static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } }