/res/raw & /assets 携带资源

来源:互联网 发布:淘宝店库存管理 编辑:程序博客网 时间:2024/04/28 23:35
/res/raw & /assets 携带资源 在android开发中,总有一些资源是想要随着安装包apk携带的,这些资源如数据库文件,文本,mp3等等。最早的做法是,在prebulid过程中,修改mk文件,将指定文件提前拷贝到系统某一文件夹目录下。这样的做法,既不科学也不美观还不安全,处于对代码的洁癖,我终于在不太忙的时候把这些资源文件带在自己的apk下。 之前说过没,由于提升速度,从文本解析调整为数据库查询。在携带文件时,这两种文件本没有不同,只是这里要讲一点inputstream的时候,二进制文件的db文件,和纯文本文件txt的方式可以不同,原因好像是,纯文本是按照unicode编码的,是16位16位的传的,二进制文件是8位传的。又想到之前ftp传输的时候,也是写的二进制传输。 言归正传,无论是raw文件夹还是assets文件夹,都是在生成apk的时候不编译而直接携带在apk的压缩包中的,这可以打开apk检验。这想必也是raw的得名。 提取的方法都是从inputstream转,转成什么形式的,要看对inputstream的操作,下篇也许会讲。具体的过程是: Java代码 1.private void getRaw(){ 2. File target = new File( "/data/data/com.android." ); 3. 4.// InputStream in = this.getResources().openRawResource(R.raw.weather_db); 5.// try { 6.// } catch (IOException e1) { 7.// // TODO Auto-generated catch block 8.// e1.printStackTrace(); 9.// } 10. InputStream in = null ; 11. OutputStream out = null ; 12. 13. BufferedInputStream bin = null ; 14. BufferedOutputStream bout = null ; 15. try { 16. ////////////////////////////////////////////////////////////////////// 17. int xx = 1 ; //R.raw.parse_weather_db_aa; 18. xx+=1 ; 19. try { 20. in = getResources().openRawResource(xx); 21. }catch (Exception e){ 22. e.printStackTrace(); 23. } 24. 25. out = new FileOutputStream(target); 26. bin = new BufferedInputStream(in); 27. bout = new BufferedOutputStream(out); 28. 29. byte [] b = new byte [ 1024 ]; 30. int len = bin.read(b); 31. 32. while (len != - 1 ){ 33. bout.write(b, 0 , len); 34. len = bin.read(b); 35. } 36.// exec("chmod 777 "+target.getAbsolutePath()); 37. } 38. catch (FileNotFoundException e){ 39. 40. e.printStackTrace(); 41. } 42. catch (IOException e) 43. { 44. 45. e.printStackTrace(); 46. } 47. finally 48. { 49. try { 50. if (bin != null ){ 51. bin.close(); 52. } 53. if (bout != null ){ 54. bout.close(); 55. } 56. } 57. catch (IOException e){ 58. e.printStackTrace(); 59. } 60. } 61. } 62. 63. private void getAsset(){ 64.// if(WeatherWidget.loadRunning(this, "FirstSetup")){ 65. if ( true ){ 66. 67. WeatherWidget.saveRunning(this , false , "FirstSetup" ); 68. 69. File databs = new File( "/data/data/com.android." ); 70. if (!databs.exists()){ 71. 72. databs.mkdir(); 73. } 74. 75. InputStream in = null ; 76. OutputStream out = null ; 77. 78. BufferedInputStream bin = null ; 79. BufferedOutputStream bout = null ; 80. 81. AssetManager am = getAssets(); 82. String asName = "parse_weather_db_aa" ; 83. 84. 85. while ( true ){ 86. 87. try { 88. 89. in = am.open(asName); 90. out = new FileOutputStream(databs.getPath()+ "/weather_db" ); 91. asName = nextAsset(asName); 92. 93. 94. bin = new BufferedInputStream(in); 95. bout = new BufferedOutputStream(out); 96. 97. byte [] b = new byte [ 8192 ]; 98. int len = bin.read(); 99. while (len != - 1 ){ 100. bout.write(b, 0 , len); 101. len = bin.read(b); 102. } 103. continue ; 104. } 105. catch (FileNotFoundException e){ 106. e.printStackTrace(); 107. try { 108. if (bin != null ){ 109. bin.close(); 110. } 111. if (bout != null ){ 112. bout.close(); 113. } 114. break ; 115. } 116. catch (IOException ee){ 117. ee.printStackTrace(); 118. } 119. break ; 120. } 121. catch (IOException e){ 122. e.printStackTrace(); 123. try { 124. if (bin != null ){ 125. bin.close(); 126. } 127. if (bout != null ){ 128. bout.close(); 129. } 130. break ; 131. } 132. catch (IOException eee){ 133. eee.printStackTrace(); 134. } 135. break ; 136. } 137. } 138. } 139. } private void getRaw(){ File target = new File("/data/data/com.android.");// InputStream in = this.getResources().openRawResource(R.raw.weather_db);// try {// } catch (IOException e1) {// // TODO Auto-generated catch block// e1.printStackTrace();// } InputStream in = null; OutputStream out = null; BufferedInputStream bin = null; BufferedOutputStream bout = null; try{ ////////////////////////////////////////////////////////////////////// int xx = 1;//R.raw.parse_weather_db_aa; xx+=1; try{ in = getResources().openRawResource(xx); }catch(Exception e){ e.printStackTrace(); } out = new FileOutputStream(target); bin = new BufferedInputStream(in); bout = new BufferedOutputStream(out); byte[] b = new byte[1024]; int len = bin.read(b); while (len != -1){ bout.write(b, 0, len); len = bin.read(b); }// exec("chmod 777 "+target.getAbsolutePath()); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try{ if (bin != null){ bin.close(); } if (bout != null){ bout.close(); } } catch (IOException e){ e.printStackTrace(); } } } private void getAsset(){// if(WeatherWidget.loadRunning(this, "FirstSetup")){ if(true){ WeatherWidget.saveRunning(this, false, "FirstSetup"); File databs = new File("/data/data/com.android."); if(!databs.exists()){ databs.mkdir(); } InputStream in = null; OutputStream out = null; BufferedInputStream bin = null; BufferedOutputStream bout = null; AssetManager am = getAssets(); String asName = "parse_weather_db_aa"; while(true){ try{ in = am.open(asName); out = new FileOutputStream(databs.getPath()+"/weather_db"); asName = nextAsset(asName); bin = new BufferedInputStream(in); bout = new BufferedOutputStream(out); byte[] b = new byte[8192]; int len = bin.read(); while (len != -1){ bout.write(b, 0, len); len = bin.read(b); } continue; } catch (FileNotFoundException e){ e.printStackTrace(); try{ if (bin != null){ bin.close(); } if (bout != null){ bout.close(); } break; } catch (IOException ee){ ee.printStackTrace(); } break; } catch (IOException e){ e.printStackTrace(); try{ if (bin != null){ bin.close(); } if (bout != null){ bout.close(); } break; } catch (IOException eee){ eee.printStackTrace(); } break; } } } } 代码比较乱,有些东西讲一遍不如需要的时候直接回来看一眼代码,去伪存真吧。 最后,讲raw & assets资源比较重要的一点,就是文件的大小限制,单个文件的大小不可以大于1M,有人说这是android的bug,呵呵,解决的方法是: 将文件split为小于1M的文件,在读取的时候outputstream不要close,而是合并写这些文件,最后就得到原始文件。分割可以用ubuntu命令, •split -b[byte] 512k [source] [prefix] 也可以用windows下文件分割器,或者直接在android中制作,代码inputstream的时候限制大小,分割存储,我没有实践过,只是看到有人是这么写的。 以下是原文,需要时搜索关键字,网址没了。 引用汉语词典开发-assets,raw的InputStream数据流操作(文件分割合并) 文章分类:移动开发 文件移动 一. 在应用中由于种种原因需要将一些外部文件放在 assets 或者 raw 文件夹内,以便进一步使用。这两个文件夹有以下的区别和联系: 1. 都是以数据流的形式进行读取,从而导致 Java 中其他的一些读取方式不能很好的作用在这些文件上面,例如 RandomAccessFile 、 FileReader 等之类的类。如果由于需要需要使用基于文件的类,则可以根据数据流创建创建临时文件( File.createTempFile )当做一个折中的办法。这是一种方法,下面将会介绍另一种方法 2. raw 文件夹中的文件不能包含有目录结构并且每个文件会映射到一个 id ,而 assets 文件夹可以有目录结构。对于对文件名敏感的程序则使用 raw 进行外部文件存储较为方便,而对于较依赖目录结构的文件则使用 assets 存储 3. 这两个文件夹中的文件都不能太大,官方数据是小于 1M 。这点需要时刻记住,因为产生的问题十分隐蔽,在程序中可以找到该文件也可以产生 inputStream 但是在读取时会抛出 IOException 异常。这中大文件需要先分割在进行读取 4. 这两个文件夹对文件名称大小写敏感,命名是尽量用小写,并且在分割合并后也要注意文件名称,否则程序会认为它们是不同的文件,但是在创建时会覆盖掉先前的文件(这点太隐蔽了, ~~~~(>_<)~~~~ ) 以下是分割数据的代码: Java代码 1. public static void CutFilesInSizeParts(InputStream fis, 2. String OutputFileName, int MaxPartSize) { 3. try { 4. 5. int TotalLength = fis.available(); 6. byte[] buffer = new byte[TotalLength + 1]; 7. int len = fis.read(buffer); 8. 9. int nbPart = len / MaxPartSize + 1; 10. int CurPos = 0; 11. 12. for (int i = 0; i < nbPart; i++) { 13. int PartLen = MaxPartSize; 14. if (CurPos + PartLen >= len) 15. PartLen = len - CurPos; 16. String outRealFileName = OutputFileName + (i + 1); 17. FileOutputStream fos = new FileOutputStream(outRealFileName); 18. fos.write(buffer, CurPos, PartLen); 19. CurPos += PartLen; 20. } 21. } catch (IOException e) { 22. e.printStackTrace(); 23. } 24. } public static void CutFilesInSizeParts(InputStream fis, String OutputFileName, int MaxPartSize) { try { int TotalLength = fis.available(); byte[] buffer = new byte[TotalLength + 1]; int len = fis.read(buffer); int nbPart = len / MaxPartSize + 1; int CurPos = 0; for (int i = 0; i < nbPart; i++) { int PartLen = MaxPartSize; if (CurPos + PartLen >= len) PartLen = len - CurPos; String outRealFileName = OutputFileName + (i + 1); FileOutputStream fos = new FileOutputStream(outRealFileName); fos.write(buffer, CurPos, PartLen); CurPos += PartLen; } } catch (IOException e) { e.printStackTrace(); } } 二,正是以上两个文件夹只能产生 InputStream 数据流,当程序有别的需要时会显的无能为力。例如在外面创建了一个 sqlite 的 db 文件,该文件需要内置到 apk 中只能放入这两个文件夹中,可是在使用时可要根据需要放置到 sdcard 中或者 database 中去。因此需要对读取文件并在相应位置生成目的文件,按照自己需要的方式进行读取,这也提供了第二种方法。这一步需要注意一下几点: 1. 文件的权限,否则可能会被别的应用使用 2. 若是要对分割后的文件进行合并,则要注意文件顺序 这里附上合并数据并拷贝的代码 Java代码 1. //合并并拷贝数据 2. public static void CreateFromRawDbFiles(File[] filelist, 3. FileOutputStream Fos) { 4. 5. try { 6. for (File file : filelist) { 7. InputStream inputFile = new FileInputStream(file); 8. int TotalLength = 0; 9. try { 10. TotalLength = inputFile.available(); 11. } catch (IOException e) { 12. } 13. // Reading and writing the file Method 1 : 14. byte[] buffer = new byte[TotalLength]; 15. int len = 0; 16. try { 17. len = inputFile.read(buffer); 18. } catch (IOException e) { 19. } 20. Fos.write(buffer,0,len); 21. inputFile.close(); 22. } 23. Fos.close(); 24. } catch (IOException e) { 25. } 26. }
原创粉丝点击