android asset mp3,Android 项目中资源文件 -- assets目录文件的访问

eb757835b6d9

assets简介

eb757835b6d9

Android项目中的assets目录结构图

如图所示,在Android Studio项目中的根目录下,就会有一个assets的文件目录(若不存在,可以自己新建)。它里面创建存储:图像、音视频、字体、数据库等等。之所以说它适合用来管理这些文件,是因为程序在编译时不会去处理这个目录下的文件,但是会将它们打包进apk中。而其他你随便创建的目录在编译时就会被直接忽略掉。还可以在assets文件中创建子目录,方便分类管理。

在assets目录的使用上有一点是要注意的:(这点之前没有说到过)

assets目录内的文件在程序打包发布以后是只读的。只可读取不可修改。

assets使用

assets目录文件的访问,不同res文件的访问。Android中提供了一个专门的 android.content.res.AssetManager 类用来对assets文件的访问。

eb757835b6d9

AssetManager类结构局部截图

从上面AssetManager类的结构图来看,他提供的api方法很多。但是我们需要重点关注的就以下几个方法:

构造方法

open() 方法

openFd() 方法

openNonAssetFd() 方法

openXmlResourceParser() 方法

构造方法

通常情况下,我们要获取AssetManager对象的实例时,不需要使用new的方式。通过下面的方式获取:

通过Context实例的getAssets()方法:mActivity.getAssets();

通过Resource实例的getAssets()方法:mActivity.getResources().getAssets();

open(string) & open(string,int) 方法

open(String fileName) 方法

/**

* Open an asset using ACCESS_STREAMING mode. This provides access to

* files that have been bundled with an application as assets -- that is,

* files placed in to the "assets" directory.

*

* @param fileName The name of the asset to open. This name can be hierarchical.

*

* @see #open(String, int)

* @see #list

*/

public @NonNull InputStream open(@NonNull String fileName) throws IOException {

return open(fileName, ACCESS_STREAMING);

}

open(@NonNull String fileName, int accessMode) 方法

/**

* Open an asset using an explicit access mode, returning an InputStream to

* read its contents. This provides access to files that have been bundled

* with an application as assets -- that is, files placed in to the

* "assets" directory.

*

* @param fileName The name of the asset to open. This name can be hierarchical.

* @param accessMode Desired access mode for retrieving the data.

*

* @see #ACCESS_UNKNOWN

* @see #ACCESS_STREAMING

* @see #ACCESS_RANDOM

* @see #ACCESS_BUFFER

* @see #open(String)

* @see #list

*/

public @NonNull InputStream open(@NonNull String fileName, int accessMode) throws IOException {

Preconditions.checkNotNull(fileName, "fileName");

synchronized (this) {

ensureOpenLocked();

final long asset = nativeOpenAsset(mObject, fileName, accessMode);

if (asset == 0) {

throw new FileNotFoundException("Asset file: " + fileName);

}

final AssetInputStream assetInputStream = new AssetInputStream(asset);

incRefsLocked(assetInputStream.hashCode());

return assetInputStream;

}

}

这两个方法的作用是一样的。都是将assets 目录下的某个文件封装成 InputStream 的形式供使用。

参数

String fileName 指某个文件在assets目录下的相对路径。如:aaa.mp3 或者 dir/file.avi

int accessMode 访问模式

访问模式

说明

ACCESS_UNKNOW

无模式。其代表的值是 0

ACCESS_RANDOM

这个不应该翻译成随机访问模式,无序访问模式会更适合一点。这种模式下文件的访问只会打开其中一段内容,然后再根据你的需要向流的前方或后方移动读取指针。其代表的值是 1

ACCESS_STREAMING

顺序读取模式。文件将会被从头部打开,然后按顺序向后面移动读取数据。其代表的值是 2

ACCESS_BUFFER

缓存读取模式。读取时会将整个文件直接读取到内存中,这种模式适合小文件的读取。其代表的值是 3

在 open(string) 中,它使用的文件读取模式是 ACCESS_STREAMING 模式。

openFd(string) 方法

/**

* Open an uncompressed asset by mmapping it and returning an {@link AssetFileDescriptor}.

* This provides access to files that have been bundled with an application as assets -- that

* is, files placed in to the "assets" directory.

*

* The asset must be uncompressed, or an exception will be thrown.

*

* @param fileName The name of the asset to open. This name can be hierarchical.

* @return An open AssetFileDescriptor.

*/

public @NonNull AssetFileDescriptor openFd(@NonNull String fileName) throws IOException {

Preconditions.checkNotNull(fileName, "fileName");

synchronized (this) {

ensureOpenLocked();

final ParcelFileDescriptor pfd = nativeOpenAssetFd(mObject, fileName, mOffsets);

if (pfd == null) {

throw new FileNotFoundException("Asset file: " + fileName);

}

return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]);

}

}

将 assets 目录中的文件以 FileDescriptor 的形式打开,返回一个 AssetFileDescriptor 实例。

openNonAssetFd(string) & openNonAssetFd(int,string) 方法

/**

* Open a non-asset as an asset by mmapping it and returning an {@link AssetFileDescriptor}.

* This provides direct access to all of the files included in an application

* package (not only its assets). Applications should not normally use this.

*

* The asset must not be compressed, or an exception will be thrown.

*

* @param fileName Name of the asset to retrieve.

*/

public @NonNull AssetFileDescriptor openNonAssetFd(@NonNull String fileName)

throws IOException {

return openNonAssetFd(0, fileName);

}

/**

* Open a non-asset as an asset by mmapping it and returning an {@link AssetFileDescriptor}.

* This provides direct access to all of the files included in an application

* package (not only its assets). Applications should not normally use this.

*

* The asset must not be compressed, or an exception will be thrown.

*

* @param cookie Identifier of the package to be opened.

* @param fileName Name of the asset to retrieve.

*/

public @NonNull AssetFileDescriptor openNonAssetFd(int cookie, @NonNull String fileName)

throws IOException {

Preconditions.checkNotNull(fileName, "fileName");

synchronized (this) {

ensureOpenLocked();

final ParcelFileDescriptor pfd =

nativeOpenNonAssetFd(mObject, cookie, fileName, mOffsets);

if (pfd == null) {

throw new FileNotFoundException("Asset absolute file: " + fileName);

}

return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]);

}

}

这个方法其实和上面的openFd() 是一样的。只不过它是跳出了 assets 目录的范围限定,是站在工程根目录的视角来打开文件的 FileDescriptor 的。换句话说,它允许打开 APK中任意位置文件的 AssetFileDescriptor 实例。

openXmlResourceParser(int,string) 方法

/**

* Retrieve a parser for a compiled XML file.

*

* @param cookie Identifier of the package to be opened.

* @param fileName The name of the file to retrieve.

*/

public @NonNull XmlResourceParser openXmlResourceParser(int cookie, @NonNull String fileName)

throws IOException {

try (XmlBlock block = openXmlBlockAsset(cookie, fileName)) {

XmlResourceParser parser = block.newParser();

// If openXmlBlockAsset doesn't throw, it will always return an XmlBlock object with

// a valid native pointer, which makes newParser always return non-null. But let's

// be paranoid.

if (parser == null) {

throw new AssertionError("block.newParser() returned a null parser");

}

return parser;

}

}

打开 assets 目录下的 xml 文件,直接返回 XmlResourceParser 实例。就是官方替我们做了从 InputStream 到 XML 解析器之间的转换。

assets使用举例

最普通的读取 assets 目录的文件

try {

InputStream is = this.getAssets().open("aaa.png");

Log.d("type1", "File available:" + is.available());

InputStream is2 = this.getResources().getAssets().open("bbb.png");

Log.d("type1", "File available2:" + is2.available());

} catch (IOException e) {

e.printStackTrace();

}

注意:最后一定不要忘记将用完了的流关闭

读取自定义目录层级的文件的方法。

try {

InputStream is = this.getAssets().open("a/b/c.png");

Log.d("type2", "File available:" + is.available());

} catch (IOException e) {

e.printStackTrace();

}

以文件描述符形式读取

//这里将一张图片以 AssetFileDescriptor 的形式读取出来,并转换成 Bitmap 显示在 ImageView 上

try {

AssetFileDescriptor afd = this.getAssets().openFd("river.png");

Log.d("type3", "File available:" + afd.getLength());

InputStream is = afd.createInputStream();

Bitmap bm = BitmapFactory.decodeStream(is);

is.close();

afd.close();

iv.setImageBitmap(bm);

} catch (IOException e) {

e.printStackTrace();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值