android提供了AssetManager来访问asset目录下的资源,

在activity中通过getAssets()获取AssetManager

常用的api如下:

1、列举路径下的资源String[] list(String path)

2、InputStream open(asset目录下的资源路径)

下面是放问asset目录下的图片的代码

  1. package com.example.qunzheng.customerview;
  2.  
  3. import android.app.Activity;
  4. import android.content.res.AssetManager;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.drawable.BitmapDrawable;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.ImageView;
  10.  
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13.  
  14. public class VisitAssetsResourceActivity extends Activity {
  15.  
  16. private ImageView imageView;
  17. private String[] images = null;
  18. private AssetManager assetManager;
  19.  
  20. private int curImageIndex = 0;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_visit_assets_resource);
  26.  
  27. imageView = (ImageView) findViewById(R.id.image);
  28. assetManager = getAssets();
  29. try {
  30. images = assetManager.list("");
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public void viewImage(View view) {
  37. if (curImageIndex >= images.length) {
  38. curImageIndex = 0;
  39. }
  40. /**
  41. * 如果图片资源还没有释放,则释放该资源,防止内存溢出
  42. */
  43. BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
  44. if (drawable != null && drawable.getBitmap() != null && !drawable.getBitmap().isRecycled()) {
  45. drawable.getBitmap().recycle();
  46. }
  47.  
  48. try {
  49. InputStream assertFile = assetManager.open(images[curImageIndex++]);
  50. imageView.setImageBitmap(BitmapFactory.decodeStream(assertFile));
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54.  
  55. }
  56.  
  57. }

  

android访问asset目录下的资源的更多相关文章

  1. Android 如何引用com.android.internal.R目录下的资源

    Android 如何引用com.android.internal.R目录下的资源 项目需求 有一个资源跟系统上的一个资源相同,想要引用它:frameworks/base/core/res/res/dr ...

  2. Android读取assets目录下的资源

    1.获取资源的输入流 资源文件 sample.txt 位于 $PROJECT_HOME/assets/ 目录下,可以在 Activity 中通过 Context.getAssets().open(“s ...

  3. Android之asset目录下文件的使用

    1. 获取AssetManager AssetManager am = context.getAssets(); 2. 列出assets目录下所有文件 String[] filePathList = ...

  4. 安卓获取Assets目录下的资源

    获取Assets目录下的资源 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 ! ...

  5. centos7限制普通用户访问单一目录下的单一文件

    要求给开发同事开设一个查看日志的账号,并限制其只能访问该目录下的单一文件 1.先新建账号 useradd loglook passwd loglook 家目录为/home/loglook 2.日志的属 ...

  6. Spring中无法访问resources目录下页面或静态资源

    1.新建项目,在 resources 目录下创建 views 目录,在 views 目录下创建 index.html 页面,项目跑起来,浏览器访问页面,提示找不到页面之类的错误提示. 2.再尝试访问图 ...

  7. SprignBoot是如何访问工程目录下的静态资源?

    ​ 目录 1.牛刀小试 1.1 图片静态资源的访问 1.2 为静态资源添加访问前缀 1.3  WelCome Page 的奇妙跳转 2.那么,SpringBoot是如何做到的呢? ​​​​​​​ 1. ...

  8. Asp.Net配置不允许通过url方式访问目录下的资源

    Asp.Net网站发布后,有部分文件为了安全性,是不能直接通过url访问获取 通常有2种做法: 1.将文件目录建立在 App_code 或者App_Data 等默认的隐藏目录下 2.将文件的目录添加到 ...

  9. Android复制assets目录下的图片到内存

    转自:http://www.chenwg.com/android/android%E5%A4%8D%E5%88%B6assets%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84 ...

随机推荐

  1. 【NOIP 2012 开车旅行】***

    题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...

  2. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value

    一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...

  3. oprofile使用方法

    安装oprofile,然后加载内核模块.#modprobe oprofile,模块加载后开始使用oprofile. 1. 首先设置监视内核,使用debuginfo提供的内核,/boot下面的内核无法使 ...

  4. 【HDOJ】2037 今年暑假不AC

    qsort排序后DP,水题.注意,数组开大点儿,把时间理解为0~23,开太小会wa. #include <stdio.h> #include <stdlib.h> #defin ...

  5. WIP Job > APP-WIP-25191 or Pending Close

    使用 Close Discrete Jobs (FORM) 关闭工单,有一工单的状态一直为PENDING CLOSE 检查 PENDING MATERIAL TRANSACTION ,PENDING ...

  6. HTML本地测试成功后上传博客注意事项

    需要注意不要跟博客已经存在的样式(CSS)或功能(JavaScript)起冲突 功能名一定不要一样 样式名尽量不一样 如果样式名一样,存在属性名的对应属性值尽量跟博客内相同

  7. 基于Hadoop Sequencefile的小文件解决方案

    一.概述 小文件是指文件size小于HDFS上block大小的文件.这样的文件会给hadoop的扩展性和性能带来严重问题.首先,在HDFS中,任何block,文件或者目录在内存中均以对象的形式存储,每 ...

  8. puTTY与SecureCRT的比较

    从网上看到别人对这两个工具的比较:从windows访问linux,除了samba之外,日常操作用得最多的大概就是PuTTY和SecureCRT Putty是免费的,SecureCRT是收费的(当然,有 ...

  9. Sharepoint 2010 根据用户权限隐藏Ribbon菜单

    转:http://xiangzhangjun2006.blog.163.com/blog/static/44140966201211715416178/   1.使用SPD打开站点,并打开母版页.默认 ...

  10. ArcSDE 10.1安装、配置、连接 (SQL Server 2008)

    转自:http://blog.csdn.net/esrichinacd/article/details/8510224 1  概述 ArcSDE 10.1的安装配置相较于ArcSDE 10.0和之前版 ...