Android笔记(四十九) Android中的资源访问——asset
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的更多相关文章
- Android笔记(十九) Android中的Fragment
通常我们使用Activity来展示界面,但是在手机上界面可能显示的很好看,但在平板上,因为平板的屏幕非常大,手机的界面放在平板上可能会出现控件被拉长.控件之间间距变大等问题.为了更好的体验效果,在Ac ...
- Android笔记(六十五) android中的动画——属性动画(propertyanimation)
补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...
- Android笔记(七十五) Android中的图片压缩
这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...
- Android笔记(六十九) 仿微信界面(一)
综合之前的Fragment和自定义组件的知识,实现微信界面 MainActivity.java package cn.lixyz.test; import android.app.Acti ...
- Android笔记(六十六) android中的动画——XML文件定义属性动画
除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...
- Android笔记(十) Android中的布局——表格布局
TableLayout运行我们使用表格的方式来排列控件,它的本质依然是线性布局.表格布局采用行.列的形式来管理控件,TableLayout并不需要明确的声明包含多少行多少列,而是通过添加TableRo ...
- Unity 游戏框架搭建 2019 (四十八/四十九) MonoBehaviourSimplify 中的消息策略完善&关于发送事件的简单封装
MonoBehaviourSimplify 中的消息策略完善 在上一篇,笔者说,MonoBehaviourSimplify 中的消息策略还有一些小问题.我们在这篇试着解决一下. 先贴出来代码: usi ...
- 论文阅读笔记四十九:ScratchDet: Training Single-Shot Object Detectors from Scratch(CVPR2019)
论文原址:https://arxiv.org/abs/1810.08425 github:https://github.com/KimSoybean/ScratchDet 摘要 当前较为流行的检测算法 ...
- Gradle 1.12用户指南翻译——第四十九章. Build Dashboard 插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
随机推荐
- np.meshgrid
- typescript - 5.接口
接口的作用: 在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据,也不 ...
- 转 perl DBI 总结
https://www.cnblogs.com/homezzm/archive/2011/07/22/2113618.html ##查看已经安装的包 #!/usr/bin/perluse strict ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- 570. Managers with at Least 5 Direct Reports 至少有5个直接汇报员工的经理
The Employee table holds all employees including their managers. Every employee has an Id, and there ...
- upgrade rubygems
gem install rubygems-update update_rubygems gem update --system gem update
- opengl读取灰度图生成三维地形并添加光照
转自:https://www.cnblogs.com/gucheng/p/10152889.html 准备第三方库 glew.freeglut.glm.opencv 准备一张灰度图 最终效果 代码如下 ...
- jvm面试常见题
背景:jvm相关题目面试必问,后面要深入的进行总结. JVM 面试知识整理 jvm调优命令 调优工具 Minor GC ,Full GC 触发条件 Minor GC触发条件:当Eden区满时,触发Mi ...
- 使用 pthread_cancel 引入的死锁问题
先来说一下 pthread_cancel 基本概念. pthread_cancel 调用并不是强制终止线程,它只提出请求.线程如何处理 cancel 信号则由目标线程自己决定,可以是忽略.可以是立即终 ...
- Hack The Box Web Pentest 2017
[20 Points] Lernaean [by [Arrexel] 问题描述: Your target is not very good with computers. Try and guess ...