Android从assets中读取文本和图片资源
2014-12-13 23:41
435 查看
/** 从assets 文件夹中读取文本数据 */
public static String getTextFromAssets(final Context context, String fileName) {
String result = "";
try {
InputStream in = context.getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, "UTF-8");
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** 从assets 文件夹中读取图片 */
public static Drawable loadImageFromAsserts(final Context ctx, String fileName) {
try {
InputStream is = ctx.getResources().getAssets().open(fileName);
return Drawable.createFromStream(is, null);
} catch (IOException e) {
if (e != null) {
e.printStackTrace();
}
} catch (OutOfMemoryError e) {
if (e != null) {
e.printStackTrace();
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return null;
}
public static String getTextFromAssets(final Context context, String fileName) {
String result = "";
try {
InputStream in = context.getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, "UTF-8");
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** 从assets 文件夹中读取图片 */
public static Drawable loadImageFromAsserts(final Context ctx, String fileName) {
try {
InputStream is = ctx.getResources().getAssets().open(fileName);
return Drawable.createFromStream(is, null);
} catch (IOException e) {
if (e != null) {
e.printStackTrace();
}
} catch (OutOfMemoryError e) {
if (e != null) {
e.printStackTrace();
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return null;
}
相关文章推荐
- android-读取Assets图片资源保存到SD - 随心
- android-读取Assets图片资源保存到SD - 随心
- Android实战,实现文本读取,实现assets下资源文本的读取,实现l列表下隐藏TextView点击显示
- android读取Assets图片资源保存到SD卡然后显示到ImageView里 实例
- android从assets和res文件夹中读取资源
- Android 读取Assets中图片
- Android 读取不同位置(drawable,asset,SDCard)的图片资源
- Unity3d开发android项目时引入jar包读取图片资源的问题
- (转)android常见问题之jni读取assets资源文件(附源码)
- Android读取assets目录下的资源
- Android读取assets目录下的资源(程序代码小片段)
- [AndroidTips]jni读取assets资源文件
- Android 获取assets文件夹中的图片资源
- android 最省内存的方式读取本地资源的图片
- Android 读取Assets中图片
- android常见问题之jni读取assets资源文件
- Android打包含有文本或者图片资源的JAR包
- Android读取assets目录下的资源
- android 读取assets下的文本文件内容