1.文件读取方式
    AssetManager.open(String filename),返回的是一个InputSteam类型的字节流,这里的filename必须是文件,而不能是文件夹,AssetManager打开资源文件的open方法是一个重载方法,可以添加一个打开方式的int参数,根据参数不同可做相应操作。

 
  2.资源文件是可以存在文件夹以及子目录

    public final String[]list(String path),返回当前目录下面的所有文件以及子目录的名称。可以通过递归遍历整个文件目录,实现所有资源文件的访问。

MainActivity.java

package cn.lixyz.iotest.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cn.lixyz.iotest.R;
import cn.lixyz.iotest.util.IOFile; public class MainActivity extends Activity implements OnClickListener { private Button bt_asset_read; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findView(); bt_asset_read.setOnClickListener(this);
} public void findView() {
bt_asset_read = (Button) findViewById(R.id.bt_asset_read);
} @Override
public void onClick(View v) {
IOFile ioFile;
switch (v.getId()) {
case R.id.bt_asset_read:
ioFile = new IOFile(this);
ioFile.readFromAsset(this);
break;
}
} }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.lixyz.iotest.activity.MainActivity" > <Button
android:id="@+id/bt_asset_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ASSET目录读取" /> </LinearLayout>

IOFile.java

package cn.lixyz.iotest.util;

import java.io.IOException;
import java.io.InputStream; import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log; public class IOFile { Context mContext; public IOFile(Context context) {
mContext = context;
} // 读取asset目录中的内容
public void readFromAsset(Context context) {
try {
// 获取asset管理器
AssetManager assetManager = context.getAssets();
// 通过asset管理器获取asset目录下的子目录下的文件
String[] filesName = assetManager.list("txts"); // 循环读出文件内容
for (int i = 0; i < filesName.length; i++) { InputStream inputStream = assetManager.open("txts/" + filesName[i]);
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String str = new String(bytes); Log.d("TTTT", filesName[i] + "的内容是:" + str);
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

Android笔记(四十九) Android中的资源访问——asset的更多相关文章

  1. Android笔记(十九) Android中的Fragment

    通常我们使用Activity来展示界面,但是在手机上界面可能显示的很好看,但在平板上,因为平板的屏幕非常大,手机的界面放在平板上可能会出现控件被拉长.控件之间间距变大等问题.为了更好的体验效果,在Ac ...

  2. Android笔记(六十五) android中的动画——属性动画(propertyanimation)

    补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...

  3. Android笔记(七十五) Android中的图片压缩

    这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...

  4. Android笔记(六十九) 仿微信界面(一)

          综合之前的Fragment和自定义组件的知识,实现微信界面 MainActivity.java package cn.lixyz.test; import android.app.Acti ...

  5. Android笔记(六十六) android中的动画——XML文件定义属性动画

    除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...

  6. Android笔记(十) Android中的布局——表格布局

    TableLayout运行我们使用表格的方式来排列控件,它的本质依然是线性布局.表格布局采用行.列的形式来管理控件,TableLayout并不需要明确的声明包含多少行多少列,而是通过添加TableRo ...

  7. Unity 游戏框架搭建 2019 (四十八/四十九) MonoBehaviourSimplify 中的消息策略完善&关于发送事件的简单封装

    MonoBehaviourSimplify 中的消息策略完善 在上一篇,笔者说,MonoBehaviourSimplify 中的消息策略还有一些小问题.我们在这篇试着解决一下. 先贴出来代码: usi ...

  8. 论文阅读笔记四十九:ScratchDet: Training Single-Shot Object Detectors from Scratch(CVPR2019)

    论文原址:https://arxiv.org/abs/1810.08425 github:https://github.com/KimSoybean/ScratchDet 摘要 当前较为流行的检测算法 ...

  9. Gradle 1.12用户指南翻译——第四十九章. Build Dashboard 插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

随机推荐

  1. 【linux】CentOS: Sudo: unable to initialize policy plugin

    背景: 在centos7 下 使用sudo 命令对www用户生成ssh秘钥 .报错 解决办法: yum remove sudo yum install sudo 在执行就ok了

  2. [译]在你的GitHub主页固定仓库

    原文来源:https://github.blog/2016-06-16-pin-repositories-to-your-github-profile/ 您现在可以在GitHub主页上展示最能代表您工 ...

  3. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. [LeetCode] 293. Flip Game 翻转游戏

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  5. [LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  6. 10 Spring框架--基于注解的IOC配置

    1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...

  7. GhostScript说明

    关于ghostscript(以下简称gs).Gs是一个地下工作者,一般用户不熟悉它,因为它上不和用户直接打交道,下不直接接触打印机.但是在打印工作中它却扮演了极为重要的解色. 一般从用户常见文件如图片 ...

  8. python学习-63 组合

    组合 1.什么是组合? 定义一个类,由数据属性构成,这几个属性又可以是通过一个类实例化的对象,这就是组合. 举例: class School: def __init__(self,name,addre ...

  9. Codeblocks中文乱码解决方法。

    如需安装包请后台留言!! Codeblocks中文乱码解决方法: 特别提示:出现中文乱码情况才执行以下操作,未出现请勿随意修改!!!! 打开Codeblocks -> 设置 -> 编辑器: ...

  10. Django总结篇

    1.0 简述http协议和常用请求头 http协议: ( 基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)) HTTP协议是Hyper Text Transfer Pro ...