Android apk携带资源之raw & assets
来源:互联网 发布:mac 查看局域网ip 编辑:程序博客网 时间:2024/04/29 00:43
/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代码 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; } } } } 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的时候限制大小,分割存储。
- Android apk携带资源之raw & assets
- apk携带资源之raw & assets
- apk携带资源之raw & assets
- /res/raw & /assets 携带资源
- /res/raw & /assets 携带资源
- android raw与assets资源
- android assets与raw资源
- android资源之res/raw和assets的异同
- android资源之res/raw和assets的异同
- Android资源 assets资源使用 raw
- Android资源目录assets及raw
- Android资源目录assets及raw
- android资源目录assets和res/raw
- Android资源文件目录assets和raw
- android资源目录---assets与res/raw的不同assets
- Android之assets资源
- Android 的assets文件资源与raw文件资源读取
- Android学习笔记之资源文件resource/raw和assets各自的特点。
- Android之基于HTTP协议的下载
- iteye的文章内容大部分已经转到csdn中了
- 深入.NET DataTable
- gVim目录浏览插件 - nerdtree
- android开发中webview保存cookie问题的解决
- Android apk携带资源之raw & assets
- android res下文件类型
- Win7/R2 原生VHD 启动 详解
- oracle sql日期比较
- android 如何实现附加现有数据库?而不是每次安装
- Oracle 11g 10g 无法启动Em的解决办法
- [转载]关系到能否成功编译 /etc /ld.so.conf ; ldconf
- 接触Symbian半个月来的体会
- prstat –a