android导入外部已存在的数据库

转载自 android 如何实现附加现有数据库?而不是每次安装的时候都创建


首先用文件分割器把数据库分割为小于1M的文件,并拷贝到raw下,然后就是在程序里把文件拷贝到sdcard卡上,并把分割后的文件合并成数据库文件。代码如下:

Java code

private Context context; 
     private String rootDirectory = "/data/data/CrazyNovelV1_0.com/data/";
     private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
     + "/crazynovel";
     private final String DATABASE_FILENAME = "crazynovel.db";

//复制小于1M的数据库程序        
        public SQLiteDatabase openDatabase()   
        {   
            try  
            {   
                // 获得dictionary.db文件的绝对路径   
                String databaseFilename = DATABASE_PATH+ "/" + DATABASE_FILENAME;   
                File dir = new File(DATABASE_PATH);   
                // 如果/sdcard/dictionary目录不中存在,创建这个目录   
                if (!dir.exists())   
                    dir.mkdir();   
                // 如果在/sdcard/dictionary目录中不存在   
                // dictionary.db文件,则从res\raw目录中复制这个文件到   
                // SD卡的目录(/sdcard/dictionary)   
             if (!(new File(databaseFilename)).exists())   
                    {   
                        //复制文件
                        copydb(R.raw.crazynovel1,DATABASE_PATH+ "/crazynovel1",944128);
                        copydb(R.raw.crazynovel2,DATABASE_PATH+ "/crazynovel2",944128);
                        copydb(R.raw.crazynovel3,DATABASE_PATH+ "/crazynovel3",944128);
                        copydb(R.raw.crazynovel4,DATABASE_PATH+ "/crazynovel4",944128);
                        copydb(R.raw.crazynovel5,DATABASE_PATH+ "/crazynovel5",944128);
                        copydb(R.raw.crazynovel6,DATABASE_PATH+ "/crazynovel6",944128);
                        copydb(R.raw.crazynovel7,DATABASE_PATH+ "/crazynovel7",944128);
                        copydb(R.raw.crazynovel8,DATABASE_PATH+ "/crazynovel8",944128);
                        copydb(R.raw.crazynovel9,DATABASE_PATH+ "/crazynovel9",944128);
                        copydb(R.raw.crazynovel10,DATABASE_PATH+ "/crazynovel10",944128);
                        copydb(R.raw.crazynovel11,DATABASE_PATH+ "/crazynovel11",944128);
                        copydb(R.raw.crazynovel12,DATABASE_PATH+ "/crazynovel12",669696);
                        


                        //合并文件
                        File[] files = new File[12];
                        files[0] = new File(DATABASE_PATH+ "/crazynovel1");
                        files[1] = new File(DATABASE_PATH+ "/crazynovel2");
                        files[2] = new File(DATABASE_PATH+ "/crazynovel3");
                        files[3] = new File(DATABASE_PATH+ "/crazynovel4");
                        files[4] = new File(DATABASE_PATH+ "/crazynovel5");
                        files[5] = new File(DATABASE_PATH+ "/crazynovel6");
                        files[6] = new File(DATABASE_PATH+ "/crazynovel7");
                        files[7] = new File(DATABASE_PATH+ "/crazynovel8");
                        files[8] = new File(DATABASE_PATH+ "/crazynovel9");
                        files[9] = new File(DATABASE_PATH+ "/crazynovel10");
                        files[10] = new File(DATABASE_PATH+ "/crazynovel11");
                        files[11] = new File(DATABASE_PATH+ "/crazynovel12");
                        

                        FileOutputStream fos = new FileOutputStream(databaseFilename);   
                        CreateFromRawDbFiles(files,fos);                    }   
            catch (Exception e)   
            {   
            }   
        }  
//复制文件
      private void copydb(int dbint,String databaseFilename,int FileSize)
      {
          try
          {
              // 获得封装dictionary.db文件的InputStream对象   
              InputStream is = context.getResources().openRawResource(dbint);   
              FileOutputStream fos = new FileOutputStream(databaseFilename);   
              byte[] buffer = new byte[FileSize];   
              int count = 0;   
              // 开始复制dictionary.db文件   
              while ((count = is.read(buffer)) > 0)   
              {   
                  fos.write(buffer, 0, count);   
              }   
              fos.close();   
              is.close();
          }
          catch(Exception e)   
          {}
      }
        
        
      //合并并拷贝数据
      private void CreateFromRawDbFiles(File[] filelist,FileOutputStream Fos)
      {
        try
          {
          for (File file : filelist)
          {
          InputStream inputFile = new FileInputStream(file);
          int TotalLength = 0;
          try 
          {
              TotalLength = inputFile.available();
          } 
          catch (IOException e)
          {
          }
    
          // Reading and writing the file Method 1 :
          byte[] buffer = new byte[TotalLength];
          int len = 0;
          try
          {
              len = inputFile.read(buffer);
          } 
          catch (IOException e)
          {
          }
          Fos.write(buffer,0,len);
          inputFile.close();
          }
          Fos.close();
         }
          catch(IOException e)
          {}
    }




由于拷贝文件和合并文件都是很费时的事情,最好大家用线程来调用上面的代码,并加进度条显示当前进度。具体就写了。希望对大家有帮助。


附上:

android导入外部已存在的数据库大于1M的数据库文件方法

地址:http://mobile.51cto.com/abased-337051.htm

你可能感兴趣的:(android涉及的知识)