doum
5 天以前 cb6aca0642ac8dd5de877ade168066d85acb589c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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;
    }
}