缘 之¥🈳的资源

apk携带资源之raw & assets_小组_ThinkSAAS
apk携带资源之raw & assets
apk携带资源之raw & assets
wo-07 10-14 dfeixtay or
/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的操作,下篇也许会讲。具体的过程是:
private void getRaw(){
File target = new File("/data/data/com.android.");
// InputStream in = this.getResources().openRawResource(R.raw.weather_db);
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
InputStream in =
OutputStream out =
BufferedInputStream bin =
BufferedOutputStream bout =
//////////////////////////////////////////////////////////////////////
int xx = 1;//R.raw.parse_weather_db_
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();
if (bin != null){
bin.close();
if (bout != null){
bout.close();
catch (IOException e){
e.printStackTrace();
private void getAsset(){
// if(WeatherWidget.loadRunning(this,"FirstSetup")){
WeatherWidget.saveRunning(this, false,"FirstSetup");
File databs = new File("/data/data/com.android.");
if(!databs.exists()){
databs.mkdir();
InputStream in =
OutputStream out =
BufferedInputStream bin =
BufferedOutputStream bout =
AssetManager am = getAssets();
String asName ="parse_weather_db_aa";
while(true){
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);
catch (FileNotFoundException e){
e.printStackTrace();
if (bin != null){
bin.close();
if (bout != null){
bout.close();
catch (IOException ee){
ee.printStackTrace();
catch (IOException e){
e.printStackTrace();
if (bin != null){
bin.close();
if (bout != null){
bout.close();
catch (IOException eee){
eee.printStackTrace();
代码比较乱,有些东西讲一遍不如需要的时候直接回来看一眼代码,去伪存真吧。
最后,讲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. 这两个文件夹对文件名称大小写敏感,命名是尽量用小写,并且在分割合并后也要注意文件名称,否则程序会认为它们是不同的文件,但是在创建时会覆盖掉先前的文件(这点太隐蔽了, ~~~~(&_&)~~~~ )
以下是分割数据的代码:
1. public static void CutFilesInSizeParts(InputStream fis,
String OutputFileName, int MaxPartSize) {
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 & nbP i++) {
int PartLen = MaxPartS
if (CurPos + PartLen &= len)
PartLen = len - CurP
String outRealFileName = OutputFileName + (i + 1);
FileOutputStream fos = new FileOutputStream(outRealFileName);
fos.write(buffer, CurPos, PartLen);
CurPos += PartL
} catch (IOException e) {
e.printStackTrace();
public static void CutFilesInSizeParts(InputStream fis,
String OutputFileName, int MaxPartSize) {
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 & nbP i++) {
int PartLen = MaxPartS
if (CurPos + PartLen &= len)
PartLen = len - CurP
String outRealFileName = OutputFileName + (i + 1);
FileOutputStream fos = new FileOutputStream(outRealFileName);
fos.write(buffer, CurPos, PartLen);
CurPos += PartL
} catch (IOException e) {
e.printStackTrace();
二,正是以上两个文件夹只能产生 InputStream 数据流,当程序有别的需要时会显的无能为力。例如在外面创建了一个 sqlite 的 db 文件,该文件需要内置到 apk 中只能放入这两个文件夹中,可是在使用时可要根据需要放置到 sdcard 中或者 database 中去。因此需要对读取文件并在相应位置生成目的文件,按照自己需要的方式进行读取,这也提供了第二种方法。这一步需要注意一下几点:
1. 文件的权限,否则可能会被别的应用使用
2. 若是要对分割后的文件进行合并,则要注意文件顺序
这里附上合并数据并拷贝的代码
1. //合并并拷贝数据
public static void CreateFromRawDbFiles(File[] filelist,
FileOutputStream Fos) {
for (File file : filelist) {
InputStream inputFile = new FileInputStream(file);
int TotalLength = 0;
TotalLength = inputFile.available();
} catch (IOException e) {
// Reading and writing the file Method 1 :
byte[] buffer = new byte[TotalLength];
int len = 0;
len = inputFile.read(buffer);
} catch (IOException e) {
Fos.write(buffer,0,len);
inputFile.close();
Fos.close();
} catch (IOException e) {
PHP开发框架
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
手机客户端
ThinkSAAS接收任何功能的Iphone(IOS)和Android手机的客户端定制开发服务
让ThinkSAAS更好,把建议拿来。您的举报已经提交成功,我们将尽快处理,谢谢!
体彩中心正在捣鬼呢。
大家还关注
(window.slotbydup=window.slotbydup || []).push({
id: '2081942',
container: s,
size: '1000,60',
display: 'inlay-fix'

我要回帖

更多关于 poweramp 2.0.9 539 的文章

 

随机推荐