转自:http://www.chenwg.com/android/android%E5%A4%8D%E5%88%B6assets%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84%E5%9B%BE%E7%89%87%E5%88%B0%E5%86%85%E5%AD%98.html

有些Android应用需要一些初始化数据,但是考虑到国内这种龟速网络和高昂的网络流量费用,可以将这些初始化数据存在数据库中,有时遇到图片的情况下,可以在初始化的阶段将assets目录下的图片复制到内存中。

下面是我实现的一个方法:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 /** * 读取Assets文件夹中的图片资源 * @param context * @param fileName * @return */ public static Bitmap getImageFromAssetsFile(Context context, String fileName) { //获取应用的包名 String packageName = context.getPackageName(); //定义存放这些图片的内存路径 String path= "/data/data/" +packageName; //如果这个路径不存在则新建 File file = new File(path); Bitmap image = null ; boolean isExist = file.exists(); if (!isExist){ file.mkdirs(); } //获取assets下的资源 AssetManager am = context.getAssets(); try { //图片放在img文件夹下 InputStream is = am.open( "img/" +fileName); image = BitmapFactory.decodeStream(is); FileOutputStream out = new FileOutputStream(path+ "/" +fileName); //这个方法非常赞 image.compress(Bitmap.CompressFormat.PNG, 100 ,out); out.flush(); out.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } return image; }

更多相关文章

  1. 【Android】Uri、UriMatcher、ContentUris详解
  2. 浅谈android的selector,背景选择器
  3. Android的selector 背景选择器
  4. Android高手进阶教程(二十二)之---Android中几种图像特效处理的
  5. Android之Universal Image Loader
  6. Android单张图片查看、单指移动、双指缩放、双击最大化或最小化
  7. 【Android】Uri、UriMatcher、ContentUris详解
  8. Android(安卓)的启动流程-转
  9. Android(安卓)Studio 出现 Gradle's dependency cache may be co

随机推荐

  1. android api (83) —— InputMethodSessi
  2. Android学习笔记——各种常用布局管理器
  3. Android(安卓)蓝牙开发(四)OPP传输文件
  4. 基于MQTT实现Android消息推送(Push)(2)
  5. android 有关怎么自己添加系统级服务 jav
  6. Android 网络(四) Retrofit学习笔记
  7. Android UI Operation in Thread
  8. Android快速开发框架介绍
  9. 百度Android开发面试题
  10. Android 五子棋开发经验