ResourceUtils
Android Resource工具类,可用于从android资源目录的raw和assets目录读取内容,如:
geFileFromAssets(Context context, String fileName) 得到assets目录下某个文件内容
geFileFromRaw(Context context, int resId) 得到raw目录下某个文件内容
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
*/
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;
}
}
}
ParcelUtils
Android Parcel工具类,可用于从parcel读取或写入特殊类型数据,如:
readBoolean(Parcel in) 从pacel中读取boolean类型数据
readHashMap(Parcel in, ClassLoader loader) 从pacel中读取map类型数据
writeBoolean(boolean b, Parcel out) 向parcel中写入boolean类型数据
writeHashMap(Map<K, V> map, Parcel out, int flags) 向parcel中写入map类型数据
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import android.os.Parcel;
import android.os.Parcelable;
/**
* ParcelUtils
*/
public class ParcelUtils {
private ParcelUtils() {
throw new AssertionError();
}
/**
* read boolean
*
* @param in
* @return
*/
public static boolean readBoolean(Parcel in) {
return in.readInt() == 1;
}
/**
* write boolean
*
* @param b
* @param out
*/
public static void writeBoolean(boolean b, Parcel out) {
out.writeInt(b ? 1 : 0);
}
/**
* Read a HashMap from a Parcel, class of key and value are both String
*
* @param in
* @return
*/
public static Map<String, String> readHashMapStringAndString(Parcel in) {
if (in == null) {
return null;
}
int size = in.readInt();
if (size == -1) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < size; i++) {
String key = in.readString();
map.put(key, in.readString());
}
return map;
}
/**
* Write a HashMap to a Parcel, class of key and value are both String
*
* @param map
* @param out
* @param flags
*/
public static void writeHashMapStringAndString(Map<String, String> map, Parcel out, int flags) {
if (map != null) {
out.writeInt(map.size());
for (Entry<String, String> entry : map.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
} else {
out.writeInt(-1);
}
}
/**
* Read a HashMap from a Parcel, class of key is String, class of Value can parcelable
*
* @param <V>
* @param in
* @param loader
* @return
*/
@SuppressWarnings("unchecked")
public static <V extends Parcelable> Map<String, V> readHashMapStringKey(Parcel in, ClassLoader loader) {
if (in == null) {
return null;
}
int size = in.readInt();
if (size == -1) {
return null;
}
Map<String, V> map = new HashMap<String, V>();
for (int i = 0; i < size; i++) {
String key = in.readString();
map.put(key, (V)in.readParcelable(loader));
}
return map;
}
/**
* Write a HashMap to a Parcel, class of key is String, class of Value can parcelable
*
* @param map
* @param out
* @param flags
*/
public static <V extends Parcelable> void writeHashMapStringKey(Map<String, V> map, Parcel out, int flags) {
if (map != null)