package com.doumee.keyCabinet.utils.update; import android.content.Context; import android.graphics.Bitmap; import android.os.Build; import android.os.Environment; import android.util.Log; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.sql.Timestamp; import java.text.DecimalFormat; /** * 文件工具类 * * @author spring sky * */ public class FileUtil { /** * 获取目录名称 * * @param url * @return FileName */ public static String getFileName(String url) { int lastIndexStart = url.lastIndexOf("/"); if (lastIndexStart != -1) { return url.substring(lastIndexStart + 1, url.length()); } else { return new Timestamp(System.currentTimeMillis()).toString(); } } /** * 文件是否存在 * * @param path * @return */ public static boolean FileIsExist(String path) { File file = new File(path); return file.exists(); } /** * 判断SD卡是否存在 * * @return boolean */ public static boolean checkSDCard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } /** * 保存文件到指定目录 * * @param context * @return 文件保存的目录 */ public static String setMkdir(Context context) { String filePath = null; if (checkSDCard()) { filePath = Environment.getExternalStorageDirectory() + File.separator + "com.ahrykj.Weddingg" + File.separator + "downloads"; } else { filePath = context.getCacheDir().getAbsolutePath() + File.separator + "com.ahrykj.Weddingg" + File.separator + "downloads"; } File file = new File(filePath); if (!file.exists()) { file.mkdirs(); Log.e("file", "目录不存在 创建目录 "); } else { Log.e("file", "目录存在"); } return filePath; } public static String getFile(Context context){ File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED);// 判断sd卡是否存在 if (sdCardExist) { if (Build.VERSION.SDK_INT>=29){ //Android10之后 sdDir = context.getExternalFilesDir(null); }else { sdDir = Environment.getExternalStorageDirectory();// 获取SD卡根目录 } } else { sdDir = Environment.getRootDirectory();// 获取跟目录 } return sdDir.toString(); } /** * 获取路径 * * @return * @throws IOException */ public static String getPath(Context context, String url) { String path = null; try { path = FileUtil.getFile(context) + File.separator + "ticketmachine.apk"; } catch (Exception e) { e.printStackTrace(); } return path; } public static long getFileSize(File f) throws Exception// 取得文件夹大小 { long size = 0; File flist[] = f.listFiles(); if (flist != null) { for (int i = 0; i < flist.length; i++) { if (flist[i].isDirectory()) { size = size + getFileSize(flist[i]); } else { size = size + flist[i].length(); } } } return size; } /** * 获取文件的大小 * * @param fileS * 文件的大小 * @return */ public static String FormetFileSize(long fileS) {// 转换文件大小 DecimalFormat df = new DecimalFormat("0.0");// #.00 String fileSizeString = ""; if (fileS < 1024) { if (fileS < 70) { fileSizeString = "0B"; } else { fileSizeString = df.format((double) fileS) + "B"; } } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "K"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "M"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "G"; } return fileSizeString; } /** * 删除该目录下的指定文件 * * @param filePath * 文件目录 * @param fileName * 文件名 * @return */ public static boolean deleteFile(String filePath, String fileName) { if (FileIsExist(filePath + fileName)) { File file = new File(filePath + fileName); return file.delete(); } return true; } /** * 删除该路径指定的文件 * * @param path * 文件路径 * @return */ public static boolean deleteFile(String path) { if (FileIsExist(path)) { File file = new File(path); return file.delete(); } return true; } /** * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 * * @param file */ public static void deleteFilesByDirectory(File file) { if (file.isFile()) { file.delete(); return; } if (file.isDirectory()) { File[] childFiles = file.listFiles(); if (childFiles == null || childFiles.length == 0) { file.delete(); return; } for (int i = 0; i < childFiles.length; i++) { deleteFilesByDirectory(childFiles[i]); } file.delete(); } } //将Bitmap转换成File public static File saveBitmapFile(Bitmap bitmap){ File file=new File("/sdcard/face.jpg");//将要保存图片的路径 try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); } catch (IOException e) { return null; } return file; } }