-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
ResourceUtils.java
135 lines (123 loc) · 4.17 KB
/
ResourceUtils.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
package cn.trinea.android.common.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
/**
* ResourceUtils
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2012-5-26
*/
public class ResourceUtils {
private ResourceUtils() {
throw new AssertionError();
}
/**
* get an asset using ACCESS_STREAMING mode. This provides access to files that have been bundled with an
* application as assets -- that is, files placed in to the "assets" directory.
*
* @param context
* @param fileName The name of the asset to open. This name can be hierarchical.
* @return
*/
public static String geFileFromAssets(Context context, String fileName) {
if (context == null || StringUtils.isEmpty(fileName)) {
return null;
}
StringBuilder s = new StringBuilder("");
try {
InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
s.append(line);
}
return s.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* get content from a raw resource. This can only be used with resources whose value is the name of an asset files
* -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color
* resources.
*
* @param context
* @param resId The resource identifier to open, as generated by the appt tool.
* @return
*/
public static String geFileFromRaw(Context context, int resId) {
if (context == null) {
return null;
}
StringBuilder s = new StringBuilder();
try {
InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
s.append(line);
}
return s.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* same to {@link ResourceUtils#geFileFromAssets(Context, String)}, but return type is List<String>
*
* @param context
* @param fileName
* @return
*/
public static List<String> geFileToListFromAssets(Context context, String fileName) {
if (context == null || StringUtils.isEmpty(fileName)) {
return null;
}
List<String> fileContent = new ArrayList<String>();
try {
InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
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;
}
}
/**
* same to {@link ResourceUtils#geFileFromRaw(Context, int)}, but return type is List<String>
*
* @param context
* @param resId
* @return
*/
public static List<String> geFileToListFromRaw(Context context, int resId) {
if (context == null) {
return null;
}
List<String> fileContent = new ArrayList<String>();
BufferedReader reader = null;
try {
InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
reader = new BufferedReader(in);
String line = null;
while ((line = reader.readLine()) != null) {
fileContent.add(line);
}
reader.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}