温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么读写assets目录中的资源文件

发布时间:2021-06-28 15:43:33 来源:亿速云 阅读:235 作者:Leah 栏目:移动开发

Android中怎么读写assets目录中的资源文件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

1.文件读取方式
AssetManager.open(String  filename),返回的是一个InputSteam类型的字节流,这里的filename必须是文件,而不能是文件夹,AssetManager打开 资源文件的open方法是一个重载方法,可以添加一个打开方式的int参数,根据参数不同可做相应操作。具体请看官方文档http://web.mit.edu/clio/MacData/afs/sipb/project/android/docs/reference/android/content/res/AssetManager.html
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();        } }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI