Android 应用程序内置资源放到assets文件夹下,可在里面新建目录,注意资源和目录不能有中文,获取资源的方法如下:

1. 用getResources().getAssets()方法得到AssetManager对象

2. assetManager.list("") 得到assets文件夹下所有文件名。如果assets文件夹里有文件夹叫xml,得到文件名的方法为assetManager.list("xml"),路径都是相对assets路径的。

3. 如果在assets文件夹下的文件夹为空文件夹,获取assets下文件名时会忽略空文件夹。

4. 在assets文件夹下影藏了3个带文件的文件夹,分别是images、sounds、webkit。images里面有两张png格式图片,图片如图:

示例代码:

    private void copyAssetsToFiles(Context context) {        String[] files;        try {            //注意:在assets文件夹下影藏了三个带文件的文件夹,分别是images、sounds、webkit            //返回数组files里面会包含这三个文件夹            files = context.getResources().getAssets().list("");        } catch (IOException e1) {            return;        }        String File_PATH = context.getFilesDir().getPath() + "/";        File mWorkingPath = new File(File_PATH);        if (!mWorkingPath.exists()) {            mWorkingPath.mkdirs();        }        for (int i = 0; i < files.length; i++) {            try {                String fileName = files[i];                File outFile = new File(mWorkingPath, fileName);                if (outFile.exists()) {                    continue;                }                InputStream in = context.getAssets().open(fileName);                OutputStream out = new FileOutputStream(outFile);                byte[] buf = new byte[1024];                int len;                while ((len = in.read(buf)) > 0) {                    out.write(buf, 0, len);                }                in.close();                out.close();            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }        }    };


更多相关文章

  1. 安卓SDK接入Unity
  2. cocos打包android遇到的那些坑
  3. android 下修改 hosts文件 及 out of memory的解决
  4. Android应用资源全面分析之三:Drawable资源
  5. Android(安卓)Studio 上传SVN忽略文件
  6. Android镜像文件ramdisk.img,system.img,userdata.img介绍
  7. 初学Android,字符串,数字,尺寸,数组资源(十二)
  8. Android(安卓)Drawable Resource学习(六)、LevelListDrawable
  9. Android中文件与文件夹的创建(file)

随机推荐

  1. 【代码】Android: 怎样设置app不被系统k
  2. android drawable 属性描述
  3. 最新版本(OpenSSL1.0.1e)libssl.so, libc
  4. Android中显示Dialog的方法
  5. 关于Android 打开新的Activity 虚拟键盘
  6. android studio异常关机后出现的问题
  7. AppBarLayout 介绍和简单使用
  8. Android 系统字体
  9. Android studio在Android 6.0下继续使用A
  10. TextView 中文文档