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文件夹资源的访问

       assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。
      1. 先在Activity里面调用getAssets() 来获取AssetManager引用
      2. 再用AssetManager的open(String fileName, int accessMode) 方法则指定读取的文件以及访问模式就能得到输入流InputStream。 
      3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close() 。
      4. 调用AssetManager.close() 关闭AssetManager。

需要注意的是,来自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文件夹资源的访问的更多相关文章

  1. Android(java)学习笔记135:Android中assets文件夹资源的访问

    Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件 ...

  2. android assets文件夹资源的访问

    1.assets文件夹里面的文件都是保持原始的文件格式 . 2.assets中的文件只可以读取而不能进行写的操作. 3.assets目录下的资源文件不会在R.java自动生成ID,所以读取assets ...

  3. assets文件夹资源的访问

    访问assets文件夹中的文件,分为以下几个步骤:1.在Activity里面调用getAssets(),获取AssetManager引用2.调用AssetManager.open(String fil ...

  4. java学习笔记——IO部分(遍历文件夹)

    用File类写的一个简单的工具,遍历文件夹,获取该文件夹下的所以文件(含子目录下的文件)和文件大小: /** * 列出指定目录下(包含其子目录)的所有文件 * @author syskey * */ ...

  5. Java学习-040-级联删除目录中的文件、目录

    之前在写应用模块,进行单元测试编码的时候,居然脑洞大开居然创建了一个 N 层的目录,到后来删除测试结果目录的时候,才发现删除不了了,提示目录过长无法删除.网上找了一些方法,也找了一些粉碎机,都没能达到 ...

  6. android开发如何获取res/raw和assets文件夹的路径

    ---恢复内容开始--- android开发如何获取res/raw和assets文件夹的路径,主要分为两种情况: 1.如果你只是拷贝动作,那么你只需要得到res/raw和assets文件输入流就可以, ...

  7. Android开发:第四日番外——Assets文件夹和RAW文件夹区别

    话说上回说到SQLite数据库,其中涉及到把已经设计好的数据库打包到APK中,提到可以放置在Assert文件夹或者RAW文件夹中,那么两者到底有什么区别呢?让我们来探究一下. 一.res/raw和as ...

  8. java学习笔记16--I/O流和文件

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note16.html,转载请注明源地址. IO(Input  Output)流 IO流用来处理 ...

  9. vue 项目中assets文件夹与static文件夹引用的区别

    首先,assets文件夹和static文件夹在vue-cli生成的项目中,都是用来存放静态资源的. 1.assets目录中的文件会被webpack处理解析为模块依赖,只支持相对路径形式.build的时 ...

随机推荐

  1. 浅谈UML——九种图(一)

    前言 学UML将近两个星期了,对UML有了一定的了解,学过的没学过的都知道UML中最最最核心的部分要数那九个图了.浅谈UML九种图. 实例 1.用例图: 什么是用例?描绘一个系统外在可见的需求情况,是 ...

  2. 快速搭建angular7 前端开发环境

    第一步:全局安装 Angular CLI (1)打开npm(终端)安装angular-cli 第二步:创造工作区和初始应用 (1)运行命令 ng new my-app 第三步:启动开发服务器 (1)c ...

  3. centos7.2上安装python3和pip19.0.3

    安装libressl 下载地址: https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.7.4.tar.gz 或者 :https://pan. ...

  4. Django反向解析与分组命名

    1.图书管理系统中使用分组或命名分组.URL的命名和反向解析: 2.删除功能三合一: urls.py文件 from app_book import views urlpatterns = [ url( ...

  5. little w and Soda(思维题)

    链接:https://ac.nowcoder.com/acm/contest/297/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  6. Selenium WebDriver原理(二):Selenium是如何操纵浏览器的?

    前言 上一篇文章<selenium webdriver 是怎么运行的>用了一个简单的例子--搭出租车,形象地讲解selenium webdriver 是如何运行的,而这一篇文章可以理解为深 ...

  7. Word中图片自动编号且与文中引用的编号对应

    当我们在进行大篇幅 word 文档的编写时, 为了节约修改文章中图片所花费的大量时间, 可以将图片自动编号,且让文中引用的顺序跟着图片顺序的变化而变化,具体操作如下: 1. 将鼠标定在欲加编号的下方, ...

  8. Java文件与io——复制文件和转换流

    字节流与字符流的区别 在所有的流操作里,字节永远是最基础的.任何基于字节的操作都是正确的.无论是文本文件还是二进制的文件. 如果确认流里面只有可打印的字符,包括英文的和各种国家的文字,也包括中文,那么 ...

  9. js中的focus()聚焦

    document.getElementById("vin").focus();document.form1.name.focus() $(document).ready(funct ...

  10. koa2 从入门到进阶之路 (五)

    之前的文章我们介绍了一下 koa 中使用 ejs 模板及页面渲染,本篇文章我们来看一下 koa post提交数据及 koa-bodyparser中间件. 在前端页面中,不免会用到 form 表单和 p ...