Android中如何读写assets目录中的资源文件

这篇文章主要讲解了“Android中如何读写assets目录中的资源文件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android中如何读写assets目录中的资源文件”吧!

成都创新互联-专业网站定制、快速模板网站建设、高性价比南昌县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式南昌县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖南昌县地区。费用合理售后完善,10年实体公司更值得信赖。

一、AssetManager读取文件常用的几个API

1.文件读取方式
AssetManager.open(String  filename),返回的是一个InputSteam类型的字节流,这里的filename必须是文件,而不能是文件夹,AssetManager打开 资源文件的open方法是一个重载方法,可以添加一个打开方式的int参数,根据参数不同可做相应操作。
2.资源文件是可以存在文件夹以及子目录
public final String[]list(String path),返回当前目录下面的所有文件以及子目录的名称。可以通过递归遍历整个文件目录,实现所有资源文件的访问。String[]  Array of strings, one for each asset. These file names are relative to  'path'. You can open the file by concatenating 'path' and a name in the  returned string (via File) and passing that to open().

二、相关实现代码
资源APK(A.apk)

Android中如何读写assets目录中的资源文件

具体实现代码片段,由于使用系统权限,生成的路径可以自己改一下B.apk

public 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();              fos.close();          }       } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } }

感谢各位的阅读,以上就是“Android中如何读写assets目录中的资源文件”的内容了,经过本文的学习后,相信大家对Android中如何读写assets目录中的资源文件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!

当前名称:Android中如何读写assets目录中的资源文件
本文路径:https://www.cdcxhl.com/article24/jicpce.html

成都网站建设公司_创新互联,为您提供域名注册全网营销推广静态网站外贸网站建设品牌网站制作搜索引擎优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设