DataHelper.java
7.2 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
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
240
241
242
243
244
245
246
247
248
package com.xdy.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class DataHelper {
private static SharedPreferences mSharedPreferences;
public static final String SP_NAME = "config";
/**
* 存储重要信息到sharedPreferences;
*
* @param key
* @param value
*/
public static void SetStringSF(Context context, String key, String value) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
mSharedPreferences.edit().putString(key, value).commit();
}
/**
* 返回存在sharedPreferences的信息
*
* @param key
* @return
*/
public static String getStringSF(Context context, String key) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
return mSharedPreferences.getString(key, null);
}
/**
* 存储重要信息到sharedPreferences;
*
* @param key
* @param value
*/
public static void SetIntergerSF(Context context, String key, int value) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
mSharedPreferences.edit().putInt(key, value).commit();
}
/**
* 返回存在sharedPreferences的信息
*
* @param key
* @return
*/
public static int getIntergerSF(Context context, String key) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
return mSharedPreferences.getInt(key, -1);
}
/**
* 清除某个内容
*/
public static void removeSF(Context context, String key) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
mSharedPreferences.edit().remove(key).commit();
}
/**
* 清除Shareprefrence
*/
public static void clearShareprefrence(Context context) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
mSharedPreferences.edit().clear().commit();
}
/**
* 将对象储存到sharepreference
*
* @param key
* @param device
* @param <T>
*/
public static <T> boolean saveDeviceData(Context context, String key, T device) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { //Device为自定义类
// 创建对象输出流,并封装字节流
ObjectOutputStream oos = new ObjectOutputStream(baos);
// 将对象写入字节流
oos.writeObject(device);
// 将字节流编码成base64的字符串
String oAuth_Base64 = new String(Base64.encode(baos
.toByteArray(), Base64.DEFAULT));
mSharedPreferences.edit().putString(key, oAuth_Base64).commit();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将对象从shareprerence中取出来
*
* @param key
* @param <T>
* @return
*/
public static <T> T getDeviceData(Context context, String key) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
T device = null;
String productBase64 = mSharedPreferences.getString(key, null);
if (productBase64 == null) {
return null;
}
// 读取字节
byte[] base64 = Base64.decode(productBase64.getBytes(), Base64.DEFAULT);
// 封装到字节流
ByteArrayInputStream bais = new ByteArrayInputStream(base64);
try {
// 再次封装
ObjectInputStream bis = new ObjectInputStream(bais);
// 读取对象
device = (T) bis.readObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return device;
}
/**
* 返回缓存文件夹
*/
public static File getCacheFile(Context context) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = null;
file = context.getExternalCacheDir();//获取系统管理的sd卡缓存文件
if (file == null) {//如果获取的为空,就是用自己定义的缓存文件夹做缓存路径
file = new File(getCacheFilePath(context));
if (!file.exists()) {
file.mkdirs();
}
}
return file;
} else {
return context.getCacheDir();
}
}
/**
* 获取自定义缓存文件地址
* @param context
* @return
*/
public static String getCacheFilePath(Context context) {
String packageName = context.getPackageName();
return "/mnt/sdcard/" + packageName;
}
/**
* 使用递归获取目录文件大小
*
* @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;
}
/**
* 使用递归删除文件夹
*
* @param dir
* @return
*/
public static boolean DeleteDir(File dir) {
if (dir == null) {
return false;
}
if (!dir.isDirectory()) {
return false;
}
File[] files = dir.listFiles();
for (File file : files) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
DeleteDir(file); // 递归调用继续删除
}
}
return true;
}
public static String BytyToString(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int num = 0;
while ((num = in.read(buf)) != -1) {
out.write(buf, 0, buf.length);
}
String result = out.toString();
out.close();
return result;
}
}