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下文本内容和读取图片的更多相关文章

  1. WPF Paragraph获取或修改文本内容

    一.说明 Paragraph继承自Block,Block继承自TextElement,在TextElement中 // // 摘要: // 获取表示元素中内容末尾的 System.Windows.Do ...

  2. php获取https下的内容

    直接用file_get_contents,会报错: $url = ('https://xxx.com"); file_get_contents($url); 错误: Warning: fil ...

  3. Android开发之异步获取并下载网络资源-下载图片和下载文本内容

    在android网络开发过程中,经常需要获取网络资源,比如下载图片,下载文本文件内容等,这个时候就需要http请求来获取相应的网络资源.首先看看实例效果图:              下载图片截图   ...

  4. wxParse解析富文本内容使点击图片可以选中并实现放大缩小

    wxParse解析富文本内容不多说,之前写过步骤介绍,主要是在使用过程中发现解析的富文本内容里有图片时有的可以点击放大缩小,有的点击却报错,找不到imgUrls. 经过排查发现:循环解析的富文本内容正 ...

  5. Jquery操作文本内容(三个方法:html()、text()、var())

    Jquery操作文本内容(三个方法:html().text().var()) 一.html()获取和设置文本内容和标签 1.获取标签里的结构和内容 $("ul").html() / ...

  6. java正则 读取html 获取标题/超链接/链接文本/内容

    java正则 读取html 获取标题/超链接/链接文本/内容 参考链接:http://yijianfengvip.blog.163.com/blog/static/175273432201142785 ...

  7. js如何获取select下拉框的value以及文本内容

    select下拉框在项目开发中是经常用到的,特别是在联级菜单方面的应用更为广泛.但是,对于一些初学者来说,如何获取下拉框子节点option的value值和文本内容,还是有一点难度的.其他的就不说了,现 ...

  8. 关于java 获取 html select标签 下拉框 option 文本内容 隐藏域

    在HTML中从多选下拉框中提取已选中选项的文本内容到后台,被这个问题难倒了. demo.jsp文件 <select id="selecttype" name"typ ...

  9. js&jquery 获取select下拉框的值、文本内容、自定义属性

      js&jquery 获取select下拉框的值.文本内容.自定义属性 CreationTime--2018年7月2日09点22分 Author:Marydon html <selec ...

随机推荐

  1. 使用rman备份异机恢复数据库

    一.RMAN备份源库注意点: 最好保留rman备份日志 $rman target / log=backup.log RMAN>run { allocate channel t1 type dis ...

  2. jdbc链接基础

    1 jdbc 链接两种方式,通过jdbc链接mysql数据库,url:jdbc:mysql://ip:端口[/database name] 通过什么驱动器,链接什么数据库,数据库的ip,连接端口,可以 ...

  3. 使用jvisualvm进行远程监控

    1. 描述 jvisualvm是JDK自带的监控工具,位于JDK bin目录下面. 默认jvisualvm不能监控远程的JVM进程,需要进行一些配置. 2. 单独在应用上配置JVM启动参数 在Java ...

  4. C数据结构-栈和队列,括号匹配举例---ShinePans

    1.栈和队列是两种特殊的线性表             运算操作被限定仅仅能在表的一端或两端插入,删除元素,故也称它们为限定的线性表结构 2.栈的基本运算 1).Stackinit(&s) 构 ...

  5. C# 改变无边框窗体尺寸大小的方法

    ; ; ; ; ; ; const int HTBOTTOMLEFT = 0x10; ; protected override void WndProc(ref Message m) { switch ...

  6. 获取Oracle数据库中字段信息

    select t.DATA_PRECISION,t.DATA_SCALE,t.DATA_LENGTH,t.DATA_TYPE,t.COLUMN_NAME, t.NULLABLE,t.DATA_DEFA ...

  7. 【转】用python实现简单的文本情感分析

    import jieba import numpy as np # 打开词典文件,返回列表 def open_dict(Dict='hahah',path = r'/Users/zhangzhengh ...

  8. python模块之XlsxWriter 详解

    Xlsx是python用来构造xlsx文件的模块,可以向excel2007+中写text,numbers,formulas 公式以及hyperlinks超链接. 可以完成xlsx文件的自动化构造,包括 ...

  9. python学习笔记(2)--sublimeText3运行python

    https://www.zhihu.com/question/22904994 知乎用户 To the knowledge 74 人赞同 如果是想在sublime里要python shell那种交互或 ...

  10. C语言 · Sine之舞

    基础练习 Sine之舞   时间限制:1.0s   内存限制:512.0MB    问题描述 最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数基本功.所以他准备和奶 ...