---
title: "apk携带资源之raw & assets"
author: "dfeixtay"
date: "2010-10-14T23:58:18Z"
tags: []
categories: ["移动开发"]
slug: "apk-raw-assets"
draft: false
lastmod: "2010-10-14T23:58:18Z"
id: 785354
url: "https://dfeixtay.iteye.com/blog/785354"
---

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的操作，下篇也许会讲。具体的过程是：


```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;
                }
            }
        }
    }
```



    代码比较乱，有些东西讲一遍不如需要的时候直接回来看一眼代码，去伪存真吧。

    最后，讲raw & assets资源比较重要的一点，就是文件的大小限制，单个文件的大小不可以大于1M，有人说这是android的bug，呵呵，解决的方法是：

    将文件split为小于1M的文件，在读取的时候outputstream不要close，而是合并写这些文件，最后就得到原始文件。分割可以用ubuntu命令，


split -b 512k  


也可以用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.     }  
>