获取Asset下文本内容和读取图片

  1  import android.content.Context;
  2 import android.content.res.AssetManager;
  3 import android.graphics.Bitmap;
  4 import android.graphics.BitmapFactory;
  5
  6 import java.io.BufferedReader;
  7 import java.io.ByteArrayOutputStream;
  8 import java.io.IOException;
  9 import java.io.InputStream;
 10 import java.io.InputStreamReader;
 11
 12
 13 public class LocalFileUtils {
 14
 15     /**
 16      * 获取Asset下文本内容
 17      * @param context
 18      * @param str
 19      * @return
 20      */
 21     public final static String getStringFormAsset(Context context, String str) {
 22         BufferedReader in = null;
 23         try {
 24             in = new BufferedReader(new InputStreamReader(context.getAssets().open(str)));
 25             String line;
 26             StringBuilder buffer = new StringBuilder();
 27             while ((line = in.readLine()) != null) {
 28                 buffer.append(line).append(‘\n‘);
 29             }
 30             return buffer.toString();
 31         } catch (IOException e) {
 32             e.printStackTrace();
 33             return "";
 34         } finally {
 35             if (in != null) {
 36                 try {
 37                     in.close();
 38                     in = null;
 39                 } catch (IOException e) {
 40                     e.printStackTrace();
 41                 }
 42             }
 43         }
 44     }
 45
 46     /**
 47      * @description 从Assets中读取图片
 48      *
 49      * @param context
 50      * @param fileName
 51      * @return 图片
 52      * @date 2015-6-11 15:00:55
 53      */
 54     public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
 55         Bitmap image = null;
 56         AssetManager am = context.getAssets();
 57         InputStream is = null;
 58         try {
 59             is = am.open(fileName);
 60             image = BitmapFactory.decodeStream(is);
 61             return image;
 62         } catch (IOException e) {
 63             e.printStackTrace();
 64             return image;
 65         } finally {
 66             if(is != null) {
 67                 try {
 68                     is.close();
 69                     is = null;
 70                 } catch (IOException e) {
 71                     e.printStackTrace();
 72                 }
 73             }
 74         }
 75     }
 76
 77     /**
 78      * 获取Raw下文本内容
 79      * @param context
 80      * @param rawId
 81      * @return
 82      */
 83     public final static String getStringFormRaw(Context context, int rawId) {
 84         ByteArrayOutputStream baos = null;
 85         InputStream in = context.getResources().openRawResource(rawId);
 86         try {
 87             baos = new ByteArrayOutputStream();
 88             byte[] buffer = new byte[1024];
 89             int len = 0;
 90             while ((len = in.read(buffer)) != -1) {
 91                 baos.write(buffer, 0, len);
 92             }
 93             baos.close();
 94             return baos.toString();
 95         } catch (Exception e) {
 96             e.printStackTrace();
 97             return "";
 98         } finally {
 99             if (baos != null) {
100                 try {
101                     baos.close();
102                     baos = null;
103                 } catch (IOException e) {
104                     e.printStackTrace();
105                 }
106             }
107         }
108     }
109
110 }
1 long startTime4 = System.currentTimeMillis();
2 String my_json = LocalFileUtils.getStringFormAsset(this, "testbean1.json");
3 for (int n = 0; n < 100000; n++) {
4    // 使用JSON 操作 工具将JSON字符串封装到实体类
5    TestBean1 toBean = my_gson.fromJson(my_json, my_type); //JsonTool.toBean(json, TestBean1.class);
6    System.out.println(toBean);
7 }
8 long endTime4 = System.currentTimeMillis() - startTime4;
9 Log.i(TAG, "gson....." + endTime4);
时间: 2024-08-22 18:58:26

获取Asset下文本内容和读取图片的相关文章

WPF Paragraph获取或修改文本内容

一.说明 Paragraph继承自Block,Block继承自TextElement,在TextElement中 // // 摘要: // 获取表示元素中内容末尾的 System.Windows.Documents.TextPointer. // // 返回结果: // 表示 System.Windows.Documents.TextElement 中内容末尾的 System.Windows.Documents.TextPointer. public TextPointer ContentEnd

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

在android网络开发过程中,经常需要获取网络资源,比如下载图片,下载文本文件内容等,这个时候就需要http请求来获取相应的网络资源.首先看看实例效果图:              下载图片截图                                                                                                下载文本文件内容截图 下面介绍如何来实现这样的开发: (1)从指定的URL获取对应的流 既然要获取网络资源,那么

php 实现从其他网站拷贝的富文本内容并将里面的图片抓取到本地

<span style="white-space:pre"> </span>function getImgByReg($str) { $list = array(); $c1 = preg_match_all('/<img\s.*?>/', $str, $m1); for($i = 0; $i < $c1; $i++) { $c2 = preg_match_all('/(\w+)\s*=\s*(?:(?:(["\'])(.*?)(?=

文本内容值

<div><span>文本内容</span></div> 1.获取设置元素内容  html( ) $("div").html("123");    // 设置div的元素内容 ("div").html( );   // <span>123</span> 2.获取设置元素文本内容  text( ) $("div").text( " 345"

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

select下拉框在项目开发中是经常用到的,特别是在联级菜单方面的应用更为广泛.但是,对于一些初学者来说,如何获取下拉框子节点option的value值和文本内容,还是有一点难度的.其他的就不说了,现在我就写一段代码,简单的说明一下如何获取value值以及文本内容(text),必要地添加了注释,希望对下拉框还不太熟悉的朋友有所帮助.html代码: 1 </HEAD> 2 <BODY> 3 4 书籍分类: 5 <select id="s1" > 6 &

获取后台富文本框内容,截取图片

1.split()  分割字符串,转化成数组 (1)分割数据中有某段字符串的数据 ,转化成数组 //拿取富文本框中的图片var div=data[0].text.split("/agriculture/uploads/");//定义一个变量存放字符串数据 var divstr="";//循环div的长度 for(var k=0;k<div.length;k++){ if(k==0){ //第一个k是字符串,文本内容 divstr+=div[k]; }else{

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

在HTML中从多选下拉框中提取已选中选项的文本内容到后台,被这个问题难倒了. demo.jsp文件 <select id="selecttype" name"type"> <option value="" selected="selected">请选择</option> <c:forEach items="${typeList}" var="typeLis

android之文件操作——读取assets和raw文件下的内容

1.分别创建assets文件夹和res/raw文件夹:(要注意的raw文件是在res下new,然后创建一个名字为raw的文件夹)        2.创建两个txt文件,复制到asset和raw文件夹中: 3.实现的效果: 4.实现代码: (1)布局文件: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.androi

Android中WebView获取网页中标题 ,内容, 图片的方法

如题,在Android中WebView获取网页中标题 ,内容, 图片的方法 首先是获取标题,在new WebChromeClient(){}中重写onReceivedTitle()方法 @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); // loge.e("__页面标题__"+title); } 获取内容,是参考的这边的 http