Android(java)学习笔记135:Android中assets文件夹资源的访问
Android资源文件分类:
Android资源文件大致可以分为两种:
第一种是res目录下存放的可编译的资源文件:
这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件比较简单,通过R.XXX.ID即可;
第二种是assets目录下存放的原生资源文件:
因为系统在编译的时候不会编译assets下的资源文件,所以我们不能通过R.XXX.ID的方式访问它们。那我么能不能通过该资源的绝对路径去访问它们呢?因为apk安装之后会放在/data/app/**.apk目录下,以apk形式存在,asset/res和被绑定在apk里,并不会解压到/data/data/YourApp目录下去,所以我们无法直接获取到assets的绝对路径,因为它们根本就没有。
还好Android系统为我们提供了一个AssetManager工具类。
查看官方API可知,AssetManager提供对应用程序的原始资源文件进行访问;这个类提供了一个低级别的API,它允许你以简单的字节流的形式打开和读取和应用程序绑定在一起的原始资源文件。
1. assets文件夹资源的访问
需要注意的是,来自Resources和Assets 中的文件只可以读取而不能进行写的操作
以下为从Raw文件中读取:
public String getFromRaw(){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
以下为直接从assets读取:
public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
2. 接下来,我们新建一个工程文件,命名为AssetsProject:
(1)工程一览图:
(2)接下来是主布局文件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" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取assets目录下的文章" /> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取assets目录下的图片" /> </LinearLayout>
主布局效果图:
辅助布局文件get_img.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取assets目录下的img文件" /> <Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" />
</LinearLayout> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
效果图:
辅助布局文件get_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取assets目录下的txt文件" /> <Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" />
</LinearLayout> <TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
效果图:
(3)MainActivity.java:
package com.himi.assetsproject; import java.io.IOException;
import java.io.InputStream; import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button txt_btn = (Button) findViewById(R.id.btn1);
Button img_btn = (Button) findViewById(R.id.btn2); txt_btn.setOnClickListener(new Button01Listener());
img_btn.setOnClickListener(new Button02Listener()); } private class Button01Listener implements OnClickListener { public void onClick(View v) {
setContentView(R.layout.get_text);
Button get_txt_btn = (Button) findViewById(R.id.btn3);
Button txt_back_btn = (Button) findViewById(R.id.btn4);
get_txt_btn.setOnClickListener(new Button03Listener());
txt_back_btn.setOnClickListener(new Button04Listener()); } } private class Button02Listener implements OnClickListener { public void onClick(View v) {
setContentView(R.layout.get_img);
Button get_img_btn = (Button) findViewById(R.id.btn5);
Button img_back_btn = (Button) findViewById(R.id.btn6); get_img_btn.setOnClickListener(new Button05Listener());
img_back_btn.setOnClickListener(new Button06Listener()); } } private class Button03Listener implements OnClickListener { public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.textview1);
AssetManager am = getAssets();
Typeface tf = Typeface.createFromAsset(am,
"fonts/DroidSansFallback.ttf"); tv.setTypeface(tf);
tv.setTextSize(15);
tv.setText(readAssetsFileString()); } } private class Button04Listener implements OnClickListener { public void onClick(View v) {
setContentView(R.layout.activity_main);
// 一旦返回主页,之前设置Button监听就无效,所有要重新设置,但是这样代码重复出现,说明这样设计思路不佳
Button txt_btn = (Button) findViewById(R.id.btn1);
Button img_btn = (Button) findViewById(R.id.btn2);
txt_btn.setOnClickListener(new Button01Listener());
img_btn.setOnClickListener(new Button02Listener());
} } private class Button05Listener implements OnClickListener { public void onClick(View v) {
ImageView img = (ImageView) findViewById(R.id.imageView1);
InputStream bitmap = null; try {
bitmap = getAssets().open("imgs/4.jpg");
Bitmap bit = BitmapFactory.decodeStream(bitmap);
img.setImageBitmap(bit);
} catch (Exception e) {
e.printStackTrace();
}
} } private class Button06Listener implements OnClickListener { public void onClick(View v) {
setContentView(R.layout.activity_main);
// 一旦返回主页,之前设置Button监听就无效,所有要重新设置,但是这样代码重复出现,说明这样设计思路不佳
Button txt_btn = (Button) findViewById(R.id.btn1);
Button img_btn = (Button) findViewById(R.id.btn2);
txt_btn.setOnClickListener(new Button01Listener());
img_btn.setOnClickListener(new Button02Listener()); } } public String readAssetsFileString() {
String str = null;
try {
InputStream is = getAssets().open("txts/01.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// public String(byte[] data, String
// charsetName):转化对应的byte[]数据为对应编码的"GB2312"字符串
// UTF-8:是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三个字节)来编码。是国际编码
// GB2312和GBK: 主要用于编解码常用汉字,GBK是一个改进版,所以能用GBK的时候一般不用GB2312 str = new String(buffer, "GBK");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
} }
(4)运行效果图:
图1:
图2:
Android(java)学习笔记135:Android中assets文件夹资源的访问的更多相关文章
- Android(java)学习笔记77:Android中assets文件夹资源的访问
Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件 ...
- android assets文件夹资源的访问
1.assets文件夹里面的文件都是保持原始的文件格式 . 2.assets中的文件只可以读取而不能进行写的操作. 3.assets目录下的资源文件不会在R.java自动生成ID,所以读取assets ...
- assets文件夹资源的访问
访问assets文件夹中的文件,分为以下几个步骤:1.在Activity里面调用getAssets(),获取AssetManager引用2.调用AssetManager.open(String fil ...
- java学习笔记——IO部分(遍历文件夹)
用File类写的一个简单的工具,遍历文件夹,获取该文件夹下的所以文件(含子目录下的文件)和文件大小: /** * 列出指定目录下(包含其子目录)的所有文件 * @author syskey * */ ...
- Java学习-040-级联删除目录中的文件、目录
之前在写应用模块,进行单元测试编码的时候,居然脑洞大开居然创建了一个 N 层的目录,到后来删除测试结果目录的时候,才发现删除不了了,提示目录过长无法删除.网上找了一些方法,也找了一些粉碎机,都没能达到 ...
- android开发如何获取res/raw和assets文件夹的路径
---恢复内容开始--- android开发如何获取res/raw和assets文件夹的路径,主要分为两种情况: 1.如果你只是拷贝动作,那么你只需要得到res/raw和assets文件输入流就可以, ...
- Android开发:第四日番外——Assets文件夹和RAW文件夹区别
话说上回说到SQLite数据库,其中涉及到把已经设计好的数据库打包到APK中,提到可以放置在Assert文件夹或者RAW文件夹中,那么两者到底有什么区别呢?让我们来探究一下. 一.res/raw和as ...
- java学习笔记16--I/O流和文件
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note16.html,转载请注明源地址. IO(Input Output)流 IO流用来处理 ...
- vue 项目中assets文件夹与static文件夹引用的区别
首先,assets文件夹和static文件夹在vue-cli生成的项目中,都是用来存放静态资源的. 1.assets目录中的文件会被webpack处理解析为模块依赖,只支持相对路径形式.build的时 ...
随机推荐
- 百度UEditor编辑器使用教程与使用方法
我们在做网站的时候,网站后台系统一般都会用到web编辑器,今天笔者就给大家推荐一款百度UEditor编辑器.关于这款百度UEditor编辑器官网上也有简单的教程,不过看着比较费劲,今天笔者就跟大家分享 ...
- android中常用的弹出提示框
转自:http://blog.csdn.net/centralperk/article/details/7493731 我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的 ...
- UVA 1637 Double Patience
题意:36张扑克,平分成9摞,两张数字一样的可以拿走,每次随机拿两张,问能拿光的概率. 解法:记忆化搜索,状态压缩.一开始我想在还没拿的时候概率是1,然后往全拿光推···样例过不去···后来觉得推反了 ...
- kettle作业(job)调用转换,设置变量,写日志到数据库中【转】
首先建立转换:从数据库表到日志 表输入的设置: 日志设置: 新建job: 转换选择刚才建好的输出日志转换.变量设置如下: 此ID就是转换中的${ID},执行job,可以看到控制台输出日 ...
- 安装GNS3-0.8.6-all-in-one时language里没有选项
初次安装使用GNS3,安装的版本是GNS3-0.8.6-all-in-one,本人也是菜鸟,安装时都是一路 Next,结果安装后运行出现了这样的问题,如图 language里是没有选项的,解决方法 把 ...
- NOIP2014 联合权值
2.联合权值 (link.cpp/c/pas) [问题描述] 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每条边的长度均为1.图上两点(u, v)的距离定义为u ...
- Django model中 双向关联问题,求帮助
Django model中 双向关联问题,求帮助 - 开源中国社区 Django model中 双向关联问题,求帮助
- light oj 1138 - Trailing Zeroes (III)【规律&&二分】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- [OC Foundation框架 - 17] copy语法
一个对象使用copy或mutableCopy方法可以创建对象的副本 1.copy 需要实现NSCopying协议 创建出来的是不可变副本,如NSString, NSArray, NSDictionar ...
- [OC Foundation框架 - 4] NSString的导出
void exportString() { NSString *str = @"Hello, 坑爹"; NSString *path = @"/Users/hello ...