Android之assets资源目录的各种操作

原文地址:https://blog.csdn.net/Ae_fring/article/details/78224432

既然是要对assets资源目录操作。首先来解释下assets是啥?


Android 中资源分为两种,


        ①、第一种是res下可编译的资源文件,这种资源文件系统会在R.java里面自动生成该资源文件的ID,(除了raw外,其他资源目录中的资源文件都会被编译),这也是为什么将APK文件解压后无法直接查看XML格式资源文件内容的原因。而assets与res/raw目录中的资源文件不会做任何处理,所以将APK解压后,这两个目录中的资源文件都会保持原样。res目录只能有一层子目录,而且这些子目录必须是预定义的,如res/layout、res/values等都是合法的,而res/abc,res/xyz并不是合法的资源目录。


        ②、第二种就是放在assets文件夹下面的原生资源文件,放在这个文件夹下面的文件不会被R文件编译,所以不能像第一种那样直接使用.Android提供了一个工具类,方便我们操作获取assets文件下的文件,在assets目录中可以建任意层次的子目录(只受操作系统的限制)。


接下来来点对assets实际的操作:

一、读取assets下的txt文件内容。

二、复制assets下文件夹中的文件到手机的其它(新建或者已有文件夹)路径中。


下面秀操作了(开始装逼-哈哈):


一、读取assets下的txt文件内容:(写成工具类,这样调用起来就方便了)

  1. //读取本地JSON字符
  2. public static String ReadDayDayString(Context context) {
  3. InputStream is = null;
  4. String msg = null;
  5. try {
  6. is = context.getResources().getAssets().open( "mprespons.txt");
  7. byte[] bytes = new byte[is.available()];
  8. is.read(bytes);
  9. msg = new String(bytes);
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. } finally {
  13. try {
  14. is.close();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. return msg;
  20. }


      上面代码中主要说下context.getResources().getAssets().open("xxx.txt");它是读取assets下对应的文件方法open()方法里面传对应打开的文件全名;

其它就是很简单的Java-IO流的操作。非常简单。


二、复制assets下文件夹中的文件(同样写成工具类):

        首先我们要复制assets下文件夹中的文件用上面的open("xxx.txt")方法肯定是不行了,你会想我用open("test\xx.txt")行不行。那你就想的太简单了。事实告诉我们当然不行抛出了IOException异常。不过好在Android 给了我们其他可执行的方法;

String[]listFiles=context.getAssets().list(rootDirFullPath);// 遍历该目录下的文件和文件夹

rootDirFullPath传的是空字符串("")时获取assets下全部文件或文件夹名 可以看到返回了一个字符串数组。这样我们就可以下一步操作了


  1. public class Util {
  2. /**
  3. * 从assets目录下拷贝整个文件夹,不管是文件夹还是文件都能拷贝
  4. *
  5. * @param context 上下文
  6. * @param rootDirFullPath 文件目录,要拷贝的目录如assets目录下有一个tessdata文件夹:
  7. * @param targetDirFullPath 目标文件夹位置如:/Download/tessdata
  8. */
  9. public static void copyFolderFromAssets(Context context, String rootDirFullPath, String targetDirFullPath) {
  10. Log.d( "Tag", "copyFolderFromAssets " + "rootDirFullPath-" + rootDirFullPath + " targetDirFullPath-" + targetDirFullPath);
  11. try {
  12. String[] listFiles = context.getAssets().list(rootDirFullPath); // 遍历该目录下的文件和文件夹
  13. for (String string : listFiles) { // 判断目录是文件还是文件夹,这里只好用.做区分了
  14. Log.d( "Tag", "name-" + rootDirFullPath + "/" + string);
  15. if (isFileByName(string)) { // 文件
  16. copyFileFromAssets(context, rootDirFullPath + "/" + string, targetDirFullPath + "/" + string);
  17. } else { // 文件夹
  18. String childRootDirFullPath = rootDirFullPath + "/" + string;
  19. String childTargetDirFullPath = targetDirFullPath + "/" + string;
  20. new File(childTargetDirFullPath).mkdirs();
  21. copyFolderFromAssets(context, childRootDirFullPath, childTargetDirFullPath);
  22. }
  23. }
  24. } catch (IOException e) {
  25. Log.d( "Tag", "copyFolderFromAssets " + "IOException-" + e.getMessage());
  26. Log.d( "Tag", "copyFolderFromAssets " + "IOException-" + e.getLocalizedMessage());
  27. e.printStackTrace();
  28. }
  29. }
  30. private static boolean isFileByName(String string) {
  31. if (string.contains( ".")) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. /**
  37. * 从assets目录下拷贝文件
  38. *
  39. * @param context 上下文
  40. * @param assetsFilePath 文件的路径名如:SBClock/0001cuteowl/cuteowl_dot.png
  41. * @param targetFileFullPath 目标文件路径如:/sdcard/SBClock/0001cuteowl/cuteowl_dot.png
  42. */
  43. public static void copyFileFromAssets(Context context, String assetsFilePath, String targetFileFullPath) {
  44. Log.d( "Tag", "copyFileFromAssets ");
  45. InputStream assestsFileImputStream;
  46. try {
  47. assestsFileImputStream = context.getAssets().open(assetsFilePath);
  48. copyFile(assestsFileImputStream, targetFileFullPath);
  49. } catch (IOException e) {
  50. Log.d( "Tag", "copyFileFromAssets " + "IOException-" + e.getMessage());
  51. e.printStackTrace();
  52. }
  53. }
  54. private static void copyFile(InputStream in, String targetPath) {
  55. try {
  56. FileOutputStream fos = new FileOutputStream( new File(targetPath));
  57. byte[] buffer = new byte[ 1024];
  58. int byteCount = 0;
  59. while ((byteCount = in.read(buffer)) != - 1) { // 循环从输入流读取
  60. // buffer字节
  61. fos.write(buffer, 0, byteCount); // 将读取的输入流写入到输出流
  62. }
  63. fos.flush(); // 刷新缓冲区
  64. in.close();
  65. fos.close();
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }

当然我们在使用前可以加上一点判断是否已经复制存在过:

  1. private void initAssets() {
  2. new Thread( new Runnable() {
  3. @Override
  4. public void run() {
  5. Log.e( "Tag", "执行");
  6. File file = new File(File_Path);
  7. if (!file.exists()) {
  8. Log.e( "Tag", "创建文件夹");
  9. file.mkdirs();
  10. }
  11. if (TextUtils.isEmpty(App.getSP().getString( "iscopy"))) {
  12. Log.e( "Tag", "复制文件");
  13. Util.copyFolderFromAssets(MainActivity. this, "tessdata", File_Path);
  14. App.getSP().put( "iscopy", "true");
  15. }
  16. }
  17. }).start();
  18. }

来点git图  项目结构图: 复制assets下tessdata文件夹中的两个文件到Download下新建的tessdata文件夹中


   好了,小弟功力有限,希望对你有那么一丢丢用。欢迎提出各种问题和指点。

最后附上小弟的github地址:github地址


猜你喜欢

转载自blog.csdn.net/dodod2012/article/details/80987253