Android(java)学习笔记77: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)学习笔记77:Android中assets文件夹资源的访问的更多相关文章
- Android(java)学习笔记135: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的时 ...
随机推荐
- 《OD大数据实战》Mahout入门实例
一.环境搭建 1. 下载 mahout-0.9-cdh5.3.6.tar.gz 2. 解压 3. mahout org.apache.mahout.clustering.syntheticcontro ...
- 洛谷P1038 神经网络
P1038 神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神 ...
- EOS区块同步源码分析之见证者
调了两天,终于把EOS的出块和DPOS共识机制的代码看了个大概,下面做个小总结.因为这一块是EOS比较核心的代码块,稍微复杂些,如果有理解错误之处,请大家见识并帮我指出来,我也会有可能随时改写这里的内 ...
- 问题:Tomcat启动产生错误严重: Error initializing endpoint java.lang.Exception
1问题描述: Tomcat启动产生错误严重: Error initializing endpoint java.lang.Exception: Socket bind failed: [730048] ...
- PAT甲级——1100 Mars Numbers (字符串操作、进制转换)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474 1100 Mars Numbers (20 分) ...
- MacOS下,Python2和Python3完美兼容使用(转)
问题阐述: MacOS默认Python版本是2.7.10,随着Python3的进一步占有市场,Python2.7也将在2020年结束维护,所以在同一台电脑上安装多个Python版本势在必行. 一.py ...
- Maven的作用到底是什么
1 . 帮你下载jar包 maven项目会有一个 pom.xml文件, 在这个文件里面,只要你添加相应配置,他就会自动帮你下载相应jar包,不用你铺天盖地的到处搜索你需要的jar包了 下面是示范配置文 ...
- t-ora issue can't login mysql
https://github.com/tora-tool/tora/issues/99 ### TOra is an open-source multi-platform database manag ...
- notepad++的tab设置为4个空格
设置--首选项--语言--制表符设置--(勾选上)替换为空格
- Problem D. Dwarf Tower spfa
http://codeforces.com/gym/100269/attachments 首先建图,然后图中每条边的权值是会变化的,是由dis[x] + dis[y] ---> dis[m ...