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的时 ...
随机推荐
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.10
Every $k\times k$ positive matrix $A=(a_{ij})$ can be realised as a Gram matrix, i.e., vectors $x_j$ ...
- ORACLE的sign函数和DECODE函数
比较大小函数 sign 函数语法:sign(n) 函数说明:取数字n的符号,大于0返回1,小于0返回-1,等于0返回0 示例:一.select sign( 100 ),sign(- 100 ),sig ...
- Codeforces 628D 数位dp
题意:d magic number(0<=d<9)的意思就是一个数,从最高位开始奇数位不是d,偶数位是d 题目问,给a,b,m,d(a<=b,m<2000)问,a,b之间有多少 ...
- HDU 1495 非常可乐 BFS搜索
题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n, 刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1; 分析:直接互相倒就完了,BFS模 ...
- Unicode中跟汉字相关的一些内容的总结陈词
UniHan 这几天琢磨着怎么方便的给汉字注音, 因为要知道具体哪些Unicode是给汉字用的, 就读了读Unicode的官方文档. 目前unicode已经发展到了7.0. 不看不知道, 发现Unic ...
- Hello,Ubuntu(安装过程中遇到的问题及解决)
2013-02-23 不折腾不舒服(>_<).在虚拟机上运行Ubuntu程序一多就明显卡顿,感觉效率不高.为了流畅使用Ubuntu,也便于将来学习Vim/Emacs,我决定在笔记本的Win ...
- mysql的group_concat的用法
1.语法:group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) eg. SELECT ID, GROUP ...
- 【转载】计算机视觉(CV)前沿国际国内期刊与会议
计算机视觉(CV)前沿国际国内期刊与会议这里的期刊大部分都可以通过上面的专家们的主页间接找到1.国际会议 2.国际期刊 3.国内期刊 4.神经网络 5.CV 6.数字图象 7.教育资源,大学 8.常见 ...
- leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)
https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...