下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中怎么读写assets目录中的资源文件
Android中怎么读写assets目录中的资源文件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面在下将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、AssetManager读取文件常用的几个API1.文件读取方式AssetManager.open(String
filename),返回的是一个InputSteam类型的字节流,这里的filename必须是文件,而不能是文件夹,AssetManager打开资源文件的open方法是一个重载方法,可以添加一个打开方式的int参数,根据参数不同可做相应操作。具体请看官方文档/clio/MacData/afs/sipb/project/android/docs/reference/android/content/res/AssetManager.html2.资源文件是可以存在文件夹以及子目录publicfinal
String[]list(String
path),返回当前目录下面的所有文件以及子目录的名称。可以通过递归遍历整个文件目录,实现所有资源文件的访问。String[]
Arrayofstrings,oneforeachasset.Thesefilenamesarerelativeto
'path'.Youcanopenthefilebyconcatenating'path'andanameinthe
returnedstring(viaFile)andpassingthattoopen().二、相关实现代码资源APK(A.apk)具体实现代码片段,由于使用系统权限,生成的路径可以自己改一下B.apkpublic
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
ctxDealFile
=
this.createPackageContext("com.zlc.ipanel",
Context.CONTEXT_IGNORE_SECURITY);
}
catch
(NameNotFoundException
e1)
{
//
TODO
Auto-generated
catch
block
e1.printStackTrace();
}
btn3.setOnClickListener(new
OnClickListener()
{
@Override
public
void
onClick(View
v)
{
//
TODO
Auto-generated
method
stub
try
{
String
uiFileName
=
"ipanelJoin";
deepFile(ctxDealFile,
uiFileName);
}
catch
(Exception
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
textView.setText("file
is
wrong");
}
}
});
//
}
public
void
deepFile(Context
ctxDealFile,
String
path)
{
try
{
String
str[]
=
ctxDealFile.getAssets().list(path);
if
(str.length
>
0)
{//如果是目录
File
file
=
new
File("/data/"
+
path);
file.mkdirs();
for
(String
string
:
str)
{
path
=
path
+
"/"
+
string;
System.out.println("zhoulc:\t"
+
path);
//
textView.setText(textView.getText()+"\t"+path+"\t");
deepFile(ctxDealFile,
path);
path
=
path.substring(0,
path.lastIndexOf('/'));
}
}
else
{//如果是文件
InputStream
is
=
ctxDealFile.getAssets().open(path);
FileOutputStream
fos
=
new
FileOutputStream(new
File("/data/"
+
path));
byte[]
buffer
=
new
byte[1024];
int
count
=
0;
while
(true)
{
count++;
int
len
=
is.read(buffer);
if
(len
==
-1)
{
break;
}
fos.write(buffer,
0,
len);
}
is.close();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 博士留学协议书模板打印
- 北京学位租赁协议书模板
- 白酒委托加工协议书模板
- 2024家庭装修合同书写,家庭装修合同书范本
- 2024劳动合同仲裁申请书范本
- 江苏省徐州一中2021-2022学年高考仿真模拟物理试卷含解析
- 江苏省苏州市四市五区2021-2022学年高三六校第一次联考物理试卷含解析
- 2024老师教育类劳动合同范本
- 2024酒店员工雇佣合同范本范文
- 2023年秋季马克思主义基本原理概论(含实践)专升本 总复习
- 园区园林绿化景观工程监理的重点和难点分析
- 初中英语教学中后进生转化案例
- 药理处方分析
- 现浇钢筋混凝土构件模板含量参考表
- 矿物加工实验技术实验指导书
- 幼儿基础笔画描红字帖
- 砖胎膜砌筑指导书
- 1-ISO9001:2015PPT
- 20以内进位加法口算卡片(共19页)
- 制药企业生产工艺过程中危险有害因素安全对策措施
- 恶魔城-苍月的十字架全怪物图鉴
评论
0/150
提交评论