获取Asset下文本内容和读取图片
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);
获取Asset下文本内容和读取图片的更多相关文章
- WPF Paragraph获取或修改文本内容
一.说明 Paragraph继承自Block,Block继承自TextElement,在TextElement中 // // 摘要: // 获取表示元素中内容末尾的 System.Windows.Do ...
- php获取https下的内容
直接用file_get_contents,会报错: $url = ('https://xxx.com"); file_get_contents($url); 错误: Warning: fil ...
- Android开发之异步获取并下载网络资源-下载图片和下载文本内容
在android网络开发过程中,经常需要获取网络资源,比如下载图片,下载文本文件内容等,这个时候就需要http请求来获取相应的网络资源.首先看看实例效果图: 下载图片截图 ...
- wxParse解析富文本内容使点击图片可以选中并实现放大缩小
wxParse解析富文本内容不多说,之前写过步骤介绍,主要是在使用过程中发现解析的富文本内容里有图片时有的可以点击放大缩小,有的点击却报错,找不到imgUrls. 经过排查发现:循环解析的富文本内容正 ...
- Jquery操作文本内容(三个方法:html()、text()、var())
Jquery操作文本内容(三个方法:html().text().var()) 一.html()获取和设置文本内容和标签 1.获取标签里的结构和内容 $("ul").html() / ...
- java正则 读取html 获取标题/超链接/链接文本/内容
java正则 读取html 获取标题/超链接/链接文本/内容 参考链接:http://yijianfengvip.blog.163.com/blog/static/175273432201142785 ...
- js如何获取select下拉框的value以及文本内容
select下拉框在项目开发中是经常用到的,特别是在联级菜单方面的应用更为广泛.但是,对于一些初学者来说,如何获取下拉框子节点option的value值和文本内容,还是有一点难度的.其他的就不说了,现 ...
- 关于java 获取 html select标签 下拉框 option 文本内容 隐藏域
在HTML中从多选下拉框中提取已选中选项的文本内容到后台,被这个问题难倒了. demo.jsp文件 <select id="selecttype" name"typ ...
- js&jquery 获取select下拉框的值、文本内容、自定义属性
js&jquery 获取select下拉框的值.文本内容.自定义属性 CreationTime--2018年7月2日09点22分 Author:Marydon html <selec ...
随机推荐
- Linux-Vim使用技巧
cd /tmp 切换到/tmp目录下面 vim shijiazhuang.txt 编辑shijiazhuang.txt文件 welcome to shijiazhuang. yu hua qu c ...
- pip install mysql-connector 安装出错
一.MySQL Connector/Python 2.2.3 的变化: 之前 mysql 官方说MySQL Connector/Python 是纯python语言写的,但是呢! 这个问题在2.2.3中 ...
- JVM虚拟机(五):JDK8内存模型—消失的PermGen
一.JVM 内存模型 根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分. 1.虚拟机栈: 每个线程有一个私有的栈,随着线程的创建而创建.栈里面存着的是一种叫“ ...
- Django Drops
1.Django Intro 2.Django Install (1) PIP安装 sudo apt-get isntall python-pip sudo pip install Django (2 ...
- POJ3187 Backward Digit Sums 【暴搜】
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4487 Accepted: 25 ...
- 输入LPCWSTR类型字符串
LPCWSTR tmp = L"xxx"; char*转到LPCWSTR LPCSTR(charTmp)
- [na]office 2010 2013卸载工具
http://www.ithome.com/html/soft/32777.htm Office 2003 || Office 2007 || Office 2010.
- Decoration2:引入Angularjs显示前台一条数据
SpringMVC内置的RestFul API格式采用的是最复杂最全面的HATEOAS规范,对于简单应用来说,前台解析起来不方便,我们下面主要想办法重新定义一种简单的RestFulAPI. (1)先是 ...
- UIView总结
转自:http://langhua9527.iteye.com/blog/1377741 如果想调用某个类的某个方法可以写成这样,这个方法来自NSObject类 performSelector: pe ...
- 内存控制函数(1)-mmap() 建立内存映射
示例1: 1.首先建立一个文本文件,名字为tmp,内容为hello world 2.编写mmap.c #include <sys/types.h> #include <sys/sta ...