java 解压assets文件,像操作File一样操作Assets资源

原标题:像操作File一样操作Assets资源

昨日美团点评发布2018年中期报告,上半年营收263亿元人民币,净亏损288亿元人民币,经调整后亏损42亿元。其中,外卖收入为160亿,毛利率为12.2%;到店、酒店业务68亿,毛利率为89.4%,新业务收入35.7亿。

作者简介

虽然今天已经是周五了,但是还有两天时间要继续加油哦。

本篇来自 猴子请来的救兵_的投稿,分享了自己的一个不错的开源库,通过封装简化了操作assets目录的api,希望对大家有所帮助。

猴子请来的救兵_的博客地址:

https://blog.csdn.net/aa464971

前言

平时开发中经常会用到Assets,可以让我们把一些资源内置在应用里,但是它使用起来比较麻烦,比如要使用Assets里面的一个文件,需要这样:

try{

finalInputStream stream = getAssets().open( "test.jpg");

} catch(IOException e) {

e.printStackTrace();

}

拿到的是InputStream,当需要复制到手机外部存储的时候,还得用FileOutputStream输出到文件,如果需要获取文件夹下面的文件:

try{

finalString[] list = getAssets().list( "test");

} catch(IOException e) {

e.printStackTrace();

}

这样获取到的只是文件夹下的文件名,如果要复制整个文件夹到手机目录至少要3步:

遍历;

拼接完整路径;

调用open输出到文件。

强迫症表示很难受,没有Java File的API那么方便,那只能撸一个轮子让它像File一样了。

下面的图是用AssetFile做的Assets文件管理器

33508242d7e7c4bbb9cefd56a6161366.gif

使用场景

先举一个我遇到的场景,在线滤镜和内置滤镜,在线滤镜需要先把配置文件下载到手机目录,内置滤镜就是放在Assets里的,为了方便管理,把两种滤镜都放到统一的目录,AssetFile先解决了一个复制文件夹的问题。

如果要获取下图的滤镜1.json,实际上是这样assetManager.open(filter/group/滤镜1/滤镜1.json),文件名和文件数量实际上是不受开发控制的,也不可能每次变化都再去修改一遍文件名或者代码,所以AssetFile将多层次文件夹的操作简单化了

4cc2739d78bb308f7b8a355a48d1deef.png

AssetFile并没有省略那些流程,只是经过封装,让它使用起来变得更简单了

基本实现

首先先来实现一下基本功能,Java的叫File,那我们就叫AssetFile,先暂时存一些基本信息,路径和文件名

publicclassAssetFile{

privateString assetPath;

privateString name;

}

我们在用File的时候是传文件路径,AssetFile也一样,假设Assets根目录有一个test.jpg文件

publicclassAssetFile{

privateString assetPath;

privateString name;

publicAssetFile(String assetPath){

this.assetPath = assetPath == null? "": assetPath;

//有/的话会去掉/

intindex = assetPath.lastIndexOf(File.separatorChar);

this.name = assetPath.substring(index + 1, assetPath.length());

}

}

newAssetFile( "test.jpg")

文件API

基本信息也赋好值了,可以说已经有一个雏形了,现在添加一些常用的API,比如exists()

当调用AssetManager.list(assetPath)的时候,如果找不到这个文件,会抛出IOException,所以只要catch到就说明这个文件不存在。

publicbooleanexists(AssetManager assetManager){

try{

assetManager.list(assetPath);

returntrue;

} catch(Exception e) {

e.printStackTrace();

returnfalse;

}

}

AssetFile file = newAssetFile( "test.jpg")

booleanexists = file.exists(context.getAssets())

加入文件夹支持

文件的API比较简单,要考虑文件夹就会有一些逻辑了

isDirectory()

现在我们加入一个isDirectory (),用来判断这个路径是否是文件夹,可以获取该路径下的子文件数组,来判断这个路径是不是一个文件夹,如果数组长度大于0说明是一个文件夹。

当然这个方法也有不准确的情况,比如你放了一个空文件夹在Assets里,但是为什么要放一个空文件夹在Assets里呢,所以这个方法还是可行的。

这里还有一个处理,assetManager.list()是把子文件的路径都添加到数组里了,所以不能每次都这样取一次子文件数组,可以用一个变量来缓存它,当它有值的时候,就直接用变量的值了。

publicclassAssetFile{

privateBoolean directory;

publicbooleanisDirectory(AssetManager assetManager){

if(directory == null) {

try{

directory = assetManager.list(assetPath).length > 0;

} catch(Exception e) {

e.printStackTrace();

directory = false;

}

}

returndirectory;

}

}

getParent()

还有一个常用的方法就是获取父文件夹,这个比较简单,通过分割路径字符串就可以实现。

比如A文件夹里放了B文件夹,B文件夹放了C文件,那路径其实就是A/B/C.jpg,通过substring从0开始截取到最后的/,截取后的A/B就是父文件夹的路径了。

publicString getParent(){

intindex = assetPath.lastIndexOf(File.separatorChar);

returnassetPath.substring( 0, index);

}

publicAssetFile getParentFile(){

returnnewAssetFile(getParent());

}

listFiles()

既然有文件夹,就肯定会需要用到listFiles(),assetManager.list()提供的只是子文件的文件名称,所以需要把它拼接成完整的路径传给AssetFile。

还可以添加一个AssetFileFilter,return false的就不添加到集合;了。当根目录的时候调用assetManager.list会把系统自带的一些文件列出来,所以这里还实现了一个SystemAssetFileFilter,用来过滤这些文件。

publicList listFiles(AssetManager assetManager){

returnlistFiles(assetManager, newSystemAssetFileFilter());

}

publicList listFiles(AssetManager assetManager, AssetFileFilter filter){

try{

String newAssetPath = TextUtils.isEmpty(assetPath) ? "": assetPath;

//先获取子文件数组

String[] list = assetManager.list(newAssetPath);

List fileList = newArrayList<>();

for( inti = 0; i < list.length; i++) {

AssetFile file = newAssetFile(newAssetPath, list[i]);

if(filter != null) {

//如果有过滤器,返回true的才添加到AssetFile集合

if(filter.accept(file)) {

fileList.add(file);

}

} else{

//否则直接添加

fileList.add(file);

}

}

returnfileList;

} catch(IOException e) {

e.printStackTrace();

}

returnnewArrayList<>();

}

AssetsManager

为了更方便的使用,可以再扩展一个AssetsManager,提供一些辅助性的方法,比如从Assets复制资源到手机目录,复制文件很简单,拿到流之后直接输出到文件,复制文件夹需要递归来达到复制多层文件夹的效果。

/**

* 复制Asset文件夹和里面的文件到手机目录

*

* @paramassetSource

* @paramoutputDir

*/

publicstaticbooleancopyAsset(AssetManager assetManager, AssetFile assetSource, File outputDir){

try{

File outputFile = newFile(outputDir, assetSource.getName());

String assetPath = assetSource.getAssetPath();

finalString[] list = assetManager.list(assetPath);

if(list.length <= 0) {

//文件

copyAssetFile(assetManager, assetPath, outputFile);

} else{

//目录

if(!outputFile.exists()) {

outputFile.mkdirs();

}

for(String child : list) {

copyAsset(assetManager, newAssetFile(assetPath, child), outputFile);

}

}

returntrue;

} catch(IOException e) {

e.printStackTrace();

}

returnfalse;

}

/**

* 复制Asset文件到手机目录

*

* @paramassetPath

* @paramoutputFile

* @return

*/

publicstaticbooleancopyAssetFile(AssetManager assetManager, String assetPath, File outputFile){

try{

InputStream is = assetManager.open(assetPath);

intbyteRead = 0;

FileOutputStream fs = newFileOutputStream(outputFile);

byte[] buffer = newbyte[ 1024];

while((byteRead = is.read(buffer)) != - 1) {

fs.write(buffer, 0, byteRead);

}

fs.close();

is.close();

returntrue;

} catch(Exception e) {

e.printStackTrace();

}

returnfalse;

}

文档

Gradle引入

implementation 'com.dyhdyh.io:asset-file:1.0.2'

AssetFile

//根目录

AssetFile root = newAssetFile();

//根目录下的test.jpg

AssetFile testFile = newAssetFile( "test.jpg");

//test文件夹下的test.jpg - test/test.jpg

AssetFile testFile = newAssetFile( "test/test.jpg");

AssetFile testFile = newAssetFile( "test", "test.jpg");

//获取完整路径

assetFile.getAssetPath();

//获取文件名称或目录名称

assetFile.getName();

//获取父级目录

assetFile.getParentFile();

//转换Uri

assetFile.getUri();

//是否文件夹

assetFile.isDirectory(getAssets());

//是否根目录

assetFile.isRootDir();

//文件是否存在

assetFile.exists(getAssets());

//获取目录下的文件数组

assetFile.listFiles(getAssets());

AssetsManager

//复制Assets里的test.jpg到手机根目录

AssetFile assetFile = newAssetFile( "test.jpg");

File outputFile = newFile(Environment.getExternalStorageDirectory(), assetFile.getName());

AssetsManager.copyAssetFile(getAssets(), assetFile.getAssetPath(), outputFile);

//复制Assets里的test文件夹到手机根目录

AssetsManager.copyAssetFile(getAssets(), "test", Environment.getExternalStorageDirectory());

项目地址:

https://github.com/dengyuhan/AssetFile返回搜狐,查看更多

责任编辑:

在Android应用程序中,assets文件夹中的文件是不会被解压到设备的文件系统中,也就是说它们没有绝对路径。因此,您不能直接获得assets文件夹中的PDF文件的路径。如果您需要使用PDF文件的路径,请考虑将其复制到设备的文件系统中,并使用文件路径来访问它。 以下是一个简单的示例代码,演示如何将assets文件夹中的PDF文件复制到设备的文件系统中: ```java try { InputStream inputStream = getAssets().open("example.pdf"); File outFile = new File(getExternalFilesDir(null), "example.pdf"); OutputStream outputStream = new FileOutputStream(outFile); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.flush(); outputStream.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } ``` 在这个例子中,文件"example.pdf"被复制到设备的外部文件目录中。一旦复制完成,您就可以使用文件路径来访问它。例如,如果您将文件复制到设备的外部文件目录中,则可以使用以下代码来访问它: ```java File file = new File(getExternalFilesDir(null), "example.pdf"); String filePath = file.getAbsolutePath(); ``` 请注意,将assets文件夹中的文件复制到设备的文件系统中可能会占用设备的存储空间,并且可能会导致应用程序安装包的大小增加。如果您的PDF文件非常大,那么这种方法可能不适用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值