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

在activity中通过getAssets()获取AssetManager

常用的api如下:

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

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

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

package com.example.qunzheng.customerview;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView; import java.io.IOException;
import java.io.InputStream; public class VisitAssetsResourceActivity extends Activity { private ImageView imageView;
private String[] images = null;
private AssetManager assetManager; private int curImageIndex = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visit_assets_resource); imageView = (ImageView) findViewById(R.id.image);
assetManager = getAssets();
try {
images = assetManager.list("");
} catch (IOException e) {
e.printStackTrace();
}
} public void viewImage(View view) {
if (curImageIndex >= images.length) {
curImageIndex = 0;
}
/**
* 如果图片资源还没有释放,则释放该资源,防止内存溢出
*/
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
if (drawable != null && drawable.getBitmap() != null && !drawable.getBitmap().isRecycled()) {
drawable.getBitmap().recycle();
} try {
InputStream assertFile = assetManager.open(images[curImageIndex++]);
imageView.setImageBitmap(BitmapFactory.decodeStream(assertFile));
} catch (IOException e) {
e.printStackTrace();
} } }

  

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. MongoDB实战指南(一):大数据与云计算

    1.1 什么大数据 具体来说,大数据技术涉及到数据的创造,存储,获取和分析,大数据的主要特点有下面几个: 数据量大.一个典型的PC机载2000年前后其存储空间可能有10GB,今天facebook一天增 ...

  2. Android笔记5-与USB HID 设备通信(一)

    1.了解 支持USB 主机(host)或者从机(accessary )模式最终是取决于设备的硬件,而与平台版本无关.我们可以通过usesfeature这个方法来查询自己的设备是否支持USB主从.   ...

  3. ServletInvocableHandlerMethod:167 - Error resolving argument

    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(R ...

  4. stream_iterator、ostream_iterator 使用初探

    STL定义了供输入及输出的iostream iterator类,称为 istream_iterator和ostream_iterator,分别支持单一型别的元素的读取和写入. 使用方法: 1.包含头文 ...

  5. 【HDOJ】1050 Moving Tables

    贪心问题,其实我觉得贪心就是合理的考虑最优情况,证明贪心可行即可.这题目没话多久一次ac.这道题需要注意房间号的奇偶性.1 3.2 4的测试数据.答案应该为20. #include <stdio ...

  6. WCF - Self Hosting

    WCF - Self Hosting Here, the WCF service is hosted in a console application. Given below is the proc ...

  7. Cannot Create Supplier Site (Address) (文档 ID 1069032.1)

    Error Address and Site Creation - Unable to create address and sites because of the following error ...

  8. linux c/c++ GDB教程详解(转)

    http://www.gnu.org/manual/ http://www.gnu.org/software/gdb/documentation/ http://sourceware.org/gdb/ ...

  9. HashSet的实现原理

    HashSet定义 public class HashSet<E> extends AbstractSet<E> implements Set<E>, Clonea ...

  10. [Tommas] SQL 中 WITH AS 的用法

    WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到: 下面的例子定义了一个 Temp 片段,Te ...