获取Asset下文本内容和读取图片
获取 图片 内容 读取 文本 Asset
2023-09-14 09:00:21 时间
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);
相关文章
- 在Java下使用NTP获取时间
- canvas获取浏览器指纹-唯一的设备标识
- js如何获取服务器端时间?
- 如何制作迅雷7的皮肤,如何获取迅雷7皮肤的图片
- c++ 使用PID获取可执行文件路径
- PHP获取IP所在地区
- JavaScript获取图片的原始尺寸
- 【图片】批量获取几万张图片
- 获取当前登录用户
- LruCache--远程图片获取与本地缓存
- 获取图片的大小(宽高):BytesIO
- CloudCompare二次开发(1)——获取指定高程的所有点
- Dev gridcontrol获取选定行,指定列单元格的内容
- 使用java代码获取新浪微博应用的access token
- atitit.图片相似度与图片查找的设计 获取图片指纹
- Atitit.获取验证码图片通过web
- 手把手教你用Python网络爬虫获取壁纸图片
- 手把手教你用Python网络爬虫获取壁纸图片
- Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略
- Crawler/ML:爬虫技术(基于urllib.request库从网页获取图片)+HierarchicalClustering层次聚类算法,实现自动从网页获取图片然后根据图片色调自动分类
- Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略
- 随机获取图片的api接口
- android:sharedUserId 获取系统权限
- LabVIEW中通过编程获取前面板的屏幕截图
- JS,Jquery获取各种屏幕的宽度和高度
- .NET MVC第四章、模型绑定获取表单数据