-
Notifications
You must be signed in to change notification settings - Fork 16
/
ResourceUtil.java
247 lines (232 loc) · 8.06 KB
/
ResourceUtil.java
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
package com.classic.android.utils;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 应用名称: BaseProject
* 包 名 称: com.classic.android.utils
*
* 文件描述: Assets、Raw资源文件操作工具类
* 创 建 人: 续写经典
* 创建时间: 2015/11/3 17:26
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public final class ResourceUtil {
private static final String CHARSET_NAME = "UTF-8";
private ResourceUtil() { }
/**
* 读取Assets下的字符串列表
*/
public static List<String> getListFromAssets(@NonNull Context context, @NonNull String fileName) {
List<String> fileContent = new ArrayList<>();
try {
InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName), CHARSET_NAME);
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
fileContent.add(line);
}
br.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 读取Raw下的字符串列表
*/
public static List<String> getListFromRaw(@NonNull Context context, int resId) {
List<String> fileContent = new ArrayList<>();
BufferedReader reader;
try {
InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId), CHARSET_NAME);
reader = new BufferedReader(in);
String line;
while ((line = reader.readLine()) != null) {
fileContent.add(line);
}
reader.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 描述:读取Assets目录的文件内容.(默认UTF-8编码)
*
* @param context the context
* @param name the name
* @return string
*/
public static String readAssetsByName(@NonNull Context context, @NonNull String name) {
return readAssetsByName(context, name, CHARSET_NAME);
}
/**
* 描述:读取Assets目录的文件内容.
*
* @param context the context
* @param name the name
* @param encoding the encoding
* @return the string
*/
public static String readAssetsByName(@NonNull Context context, @NonNull String name, @NonNull String encoding) {
String text = null;
InputStreamReader inputReader = null;
BufferedReader bufReader = null;
try {
inputReader = new InputStreamReader(context.getAssets().open(name), encoding);
bufReader = new BufferedReader(inputReader);
String line;
StringBuilder buffer = new StringBuilder();
while ((line = bufReader.readLine()) != null) {
buffer.append(line);
}
text = new String(buffer.toString().getBytes(encoding), encoding);
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseUtil.close(bufReader, inputReader);
}
return text;
}
/**
* 描述:读取Raw目录的文件内容.(默认UTF-8编码)
*
* @param context the context
* @param id the id
* @return string
*/
public static String readRawByName(@NonNull Context context, int id) {
return readRawByName(context, id, CHARSET_NAME);
}
/**
* 描述:读取Raw目录的文件内容.
*
* @param context the context
* @param id the id
* @param encoding the encoding
* @return the string
*/
public static String readRawByName(@NonNull Context context, int id, @NonNull String encoding) {
String text = null;
InputStreamReader inputReader = null;
BufferedReader bufReader = null;
try {
inputReader = new InputStreamReader(context.getResources().openRawResource(id), encoding);
bufReader = new BufferedReader(inputReader);
String line;
//noinspection StringBufferMayBeStringBuilder
StringBuffer buffer = new StringBuffer();
while ((line = bufReader.readLine()) != null) {
buffer.append(line);
}
text = new String(buffer.toString().getBytes(encoding), encoding);
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseUtil.close(bufReader, inputReader);
}
return text;
}
/**
* 描述:获取Asset中的图片资源.
*
* @param context the context
* @param fileName the file name
* @return Bitmap 图片
*/
public static Bitmap getBitmapFromAsset(@NonNull Context context, @NonNull String fileName) {
Bitmap bit = null;
try {
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open(fileName);
bit = BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
return bit;
}
/**
* 描述:获取Asset中的图片资源.
*
* @param context the context
* @param fileName the file name
* @return Drawable 图片
*/
public static Drawable getDrawableFromAsset(@NonNull Context context, @NonNull String fileName) {
Drawable drawable = null;
try {
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open(fileName);
drawable = Drawable.createFromStream(is, null);
} catch (Exception e) {
e.printStackTrace();
}
return drawable;
}
/**
* 拷贝Assets目录内容到sd卡目录
*
* @param outDir 完整sd卡路径
*/
public static void copyAssets2SD(@NonNull Context context, @NonNull String assetDir, @NonNull String outDir) {
String[] files;
InputStream is = null;
OutputStream os = null;
try {
files = context.getAssets().list(assetDir);
File outDirFile = new File(outDir);
boolean isDirCreated = false;
if (!outDirFile.exists()) {
isDirCreated = outDirFile.mkdirs();
}
if (!isDirCreated) {
return;
}
for (String fileName : files) {
String[] filesChild = context.getAssets().list(fileName);
if (filesChild != null && filesChild.length > 0) {
copyAssets2SD(context, fileName, outDir + "/" + fileName);
} else {
if (!TextUtils.isEmpty(assetDir)) {
is = context.getAssets().open(assetDir + "/" + fileName);
} else {
is = context.getAssets().open(fileName);
}
File outFile = new File(outDir + "/" + fileName);
if (outFile.exists()) {
boolean result = outFile.delete();
}
if (outFile.createNewFile()) {
os = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseUtil.close(is);
CloseUtil.close(os);
}
}
}