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. UVA 558 Wormholes

    要问是否存在一个总权重为负数的环,用dfs即可解决. time:33ms #include <cstdio> #include <cstring> #define N 3000 ...

  2. 解决win8.1右键菜单出现在左边

    这个问题估计很少有人遇到,当在桌面上单击鼠标右键时,如果正常情况下,应该是在鼠标光标的右侧弹出来,除非右边的空间不够了,才在左侧弹出.但遇到故障,就是不论在桌面的哪里点右键,菜单都在左侧弹出,虽然不影 ...

  3. 使用Sqoop从mysql向hdfs或者hive导入数据时出现的一些错误

    1.原表没有设置主键,出现错误提示: ERROR tool.ImportTool: Error during import: No primary key could be found for tab ...

  4. restful理解

    越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...

  5. linux下跨服务器文件文件夹的复制

    文件的复制:scp –P (端口号) ./authorized_keys berchina@hadoop002:/home/berchina 文件夹的复制:scp -r -P (端口号) /home/ ...

  6. IPv6 tutorial 4 IPv6 address syntax

    https://4sysops.com/archives/ipv6-tutorial-part-4-ipv6-address-syntax/ Now that you know about the n ...

  7. 指令 scope

    angular学习笔记(三十)-指令(8)-scope <!DOCTYPE html> <html ng-app="myApp"> <head> ...

  8. JavaScript window.location对象

    JavaScript window.location对象   示例 注意 方法 经常使用window.location,它的结构总是记不住,简单梳理下,方便以后查询. 示例 URL:http://b. ...

  9. 一例胜千言,详谈SQL Sever数据库锁

    1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一 ...

  10. Android PagerAdapter

    本基类是ViewPager提供填充页面内容的适配器(数据与显示内容之间桥梁).通常,人们不是直接使用本基类的,而是使用这个基类的实现:FragmentPagerAdapter   或者Fragment ...