gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:34:37 25 4
gpt4 key购买 nike

我只需要在运行时在单个 .apk 中绑定(bind) .db 和 .apk,这样用户就可以下载并运行应用程序。我不需要。 db in assets,raw,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();

}

代码不是我的,而是完整教程的一部分:Using your own DB in Android

如果您希望在/raw/中放置不同的数据库文件,您可能希望在市场允许的情况下使用多个 APK,或者制作一个 ant 脚本以在构建 APK 时选择不同的数据库。

关于android - 如何将 .apk 和 .db 绑定(bind)在一个 .apk 中,以便用户可以下载和运行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7088237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com