获取Asset下文本内容和读取图片
2024-09-29 22:08:50
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; public class LocalFileUtils { /**
* 获取Asset下文本内容
* @param context
* @param str
* @return
*/
public final static String getStringFormAsset(Context context, String str) {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(context.getAssets().open(str)));
String line;
StringBuilder buffer = new StringBuilder();
while ((line = in.readLine()) != null) {
buffer.append(line).append('\n');
}
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return "";
} finally {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* @description 从Assets中读取图片
*
* @param context
* @param fileName
* @return 图片
* @date 2015-6-11 15:00:55
*/
public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
Bitmap image = null;
AssetManager am = context.getAssets();
InputStream is = null;
try {
is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
return image;
} catch (IOException e) {
e.printStackTrace();
return image;
} finally {
if(is != null) {
try {
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 获取Raw下文本内容
* @param context
* @param rawId
* @return
*/
public final static String getStringFormRaw(Context context, int rawId) {
ByteArrayOutputStream baos = null;
InputStream in = context.getResources().openRawResource(rawId);
try {
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
return baos.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
if (baos != null) {
try {
baos.close();
baos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }
long startTime4 = System.currentTimeMillis();
String my_json = LocalFileUtils.getStringFormAsset(this, "testbean1.json");
for (int n = 0; n < 100000; n++) {
// 使用JSON 操作 工具将JSON字符串封装到实体类
TestBean1 toBean = my_gson.fromJson(my_json, my_type); //JsonTool.toBean(json, TestBean1.class);
System.out.println(toBean);
}
long endTime4 = System.currentTimeMillis() - startTime4;
Log.i(TAG, "gson....." + endTime4);
最新文章
- Linux文件查找命令 find 详解
- java环境配置步骤
- R中list对象属性以及具有list性质的对象
- 安装eclipse插件时出现问题
- C# webBrowser(wpf/winform) 互调js
- Git.Framework 框架随手记--ORM条件组合
- SAP ABAP程序下载器(增强版)
- HTTP协议--简析
- python mysql 2014 Commands out of sync; you can&#39;t run this command now
- BZOJ 1923: [Sdoi2010]外星千足虫 [高斯消元XOR]
- git关联了无用的,取消关联,并重置gitignore
- hdu 1576 A/B 【扩展欧几里得】【逆元】
- Django模型层之多表操作
- node集成mysql——pool连接池
- 从一个activity返回
- Windows C++ 非递归式(stack)深度优先遍历目录
- html 1.0 鼠标放上去会亮 onmouseover onmouseout 以及this标签的使用
- localhost不能访问127.0.0.1可以访问的原因以及解决办法
- jQuery插件——1.编写规则
- Django之auth模块(用户认证)登陆组件
热门文章
- FFmpeg 如何探测网络流格式/如何从内存中获取数据
- 解析URL中的携带的参数到Map
- 雅虎天气API调用
- Webkit初始化以及载入URL过程中各种对象的建立时序以及DOM树的建立详情分析
- [Jobdu] 题目1390:矩形覆盖
- Jackson 时间格式化,时间注解 @JsonFormat 与 @DatetimeFormat 用法、时差问题说明
- 实战 Lucene,第 1 部分: 初识 Lucene (zhuan)
- 关于Context
- 一款基于jQuery和CSS3炫酷3D旋转画廊特效插件
- jquery autocomplete s.toLowerCase(); 对象不支持此属性或方法