FileUtil.java
3.95 KB
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
package com.hjx.personalcenter.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* Created by yf on 2016/7/22 0022.
* 文件的缓存与读取
*/
public class FileUtil {
private static final String TAG = "FileUtil";
/**
* 获取缓存路径
*
* @param context
* @return
*/
public static String getCachePath(Context context) {
String cachePath = null;
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return cachePath;
}
/**
* 删除指定文件
*
* @param file
* @return
*/
public static boolean deleteFile(File file) {
if (file.exists() && file.isFile()) {
return file.delete();
}
return false;
}
/**
* 递归删除文件和文件夹
*
* @param file 要删除的根目录
*/
public static void recursionDeleteFile(File file) {
if (file.isFile()) {
file.delete();
return;
}
if (file.isDirectory()) {
File[] childFile = file.listFiles();
if (childFile == null || childFile.length == 0) {
file.delete();
return;
}
for (File f : childFile) {
recursionDeleteFile(f);
}
file.delete();
}
}
/**
* 压缩图片方法,为了上传到服务器,保证图片大小在100k一下
*
* @param context
* @param fileSrc
* @return
*/
public static File getSmallBitmap(Context context, String fileSrc) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileSrc, options);
options.inSampleSize = calculateInSampleSize(options, 480, 800);
Log.i(TAG, "options.inSampleSize-->" + options.inSampleSize);
options.inJustDecodeBounds = false;
Bitmap img = BitmapFactory.decodeFile(fileSrc, options);
Log.i(TAG, "file size after compress-->" + img.getByteCount() / 256);
String filename = context.getFilesDir() + File.separator + "video-" + img.hashCode() + ".jpg";
saveBitmap2File(img, filename);
return new File(filename);
}
/**
* 设置压缩的图片的大小设置的参数
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
int heightRatio = Math.round(height) / reqHeight;
int widthRatio = Math.round(width) / reqWidth;
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
/**
* 保存bitmap到文件
*
* @param bmp
* @param filename
* @return
*/
public static boolean saveBitmap2File(Bitmap bmp, String filename) {
Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
int quality = 50;//压缩50% 100表示不压缩
OutputStream stream = null;
try {
stream = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmp.compress(format, quality, stream);
}
}