如何将.apk和.db绑定在一个.apk中,以便用户可以下载和运行应用程序



所有我需要绑定。db和。apk在运行时在单个。apk,以便用户可以下载和运行应用程序。我不需要。db在资产,原始的,URL路径,因为我们需要改变。db在运行时,我们有这么多的用户和不同的用户,我们有不同的。db。它是基于ERP的应用程序。提前感谢

将.db放入/raw/目录。

然后在运行时将其复制到位于应用程序内存中的新数据库中,然后可以修改。

  //By calling this method and empty database will be created into the default system path
  //of your application so we are gonna be able to overwrite that database with our database.
  this.getReadableDatabase();
  /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{
        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;
        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);
        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

代码不是我的,而是来自完整教程:在Android中使用您自己的DB

如果你想把不同的数据库文件放在你的/raw/中,你可能想使用多个APK,现在市场允许这样做,或者在APK构建时制作一个ant脚本来选择不同的DB。

最新更新