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. operator new 和 operator delete 实现一个简单内存泄漏跟踪器

    先来说下实现思路:可以实现一个Trace类,调用 operator new 的时候就将指向分配内存的指针.当前文件.当前行等信息添加进Trace 成员map容器内,在调用operator delete ...

  2. android的通知栏的实现

    package com.example.mynotification; import android.os.Bundle; import android.app.Activity; import an ...

  3. SIM800L透传模式配置

    UART1_SendString("AT+CIPCLOSE=1"); //关闭连接 delay_ms(100); Second_AT_Command("AT+CIPSHU ...

  4. 了解C#文件操作

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  5. 点滴积累【other】---HTTP Error 503. The service is unavailable (转载)

    此文参考来源:http://luowei1371984.blog.163.com/blog/static/440415892012726448381/ 描述:在访问网站是会遇到这样的问题,提示“HTT ...

  6. atitit.报表最佳实践oae 与报表引擎选型

    atitit.报表最佳实践oae 与报表引擎选型 1. 报表的主要的功能and结构 2 1.1. 查询设计器(配置化,metadata in html) ,anno 2 1.2. 查询引擎 2 1.3 ...

  7. Quartus调用Modelsim SE避免重复编译Altera器件库的方法

    最近用Quartus 15.0配合Modelsim SE 10.4的64位版本,简直就是闪电一般的仿真速度.但是众所周知,SE版本最大的问题就是每次由Quartus自动调用时,都要重新编译所使用的器件 ...

  8. RFID Hacking–资源大合集

    原文: http://www.freebuf.com/news/others/605.html http://www.proxmark.org/forum/index.php RFID破解神器官方论坛 ...

  9. lua错误收集

    这里放一些我遇到的lua错误,希望大家分享一些错误给我,统一放在这里. 1.lua表的引用传值 上面的代码运行后会发现t2[2],t2[3]表里的内容也被删除了,实际上它们 与t2[1]表里的内容都是 ...

  10. svn:ignore 的用处

    用svn管理代码,一直以来都受到一件不爽事情的困扰: 1)有些文件或文件夹不想在commit的时候看到,虽然他们是non-versioned,比如*.bak.*.class,*.scc(vss文件), ...