将数据库嵌入到分布式应用程序的.apk中[Android] [英] Embed a database in the .apk of a distributed application [Android]

查看:134
本文介绍了将数据库嵌入到分布式应用程序的.apk中[Android]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我认为很简单,但我不认为答案将是...
我有很多内容在我的应用程序,以使其正常运行,我在想将其全部放入数据库中,并将在嵌入数据库中分发与市场中的应用程序。
唯一的麻烦是我不知道如何做。
我知道我可以从Eclipse DDMS提取文件.db与我的数据库的内容,我想我需要把它放在我的应用程序的资产文件夹,但如何使应用程序使用它重新生成应用程序数据库?
如果你有一些代码或帮助的链接,这将是巨大的。
感谢

My question is I think quite simple but I don't think the answer will be... I have quite a lot of content to have in my application to make it run properly, and I'm thinking of putting all of it in a database, and distribute it in an embeded database with the application in the market. The only trouble is that I have no idea of how to do that. I know that I can extract a file .db from Eclipse DDMS with the content of my database, and I suppose I need to put it in the asset folder of my application, but then how to make the application use it to regenerate the application database? If you have any link to some code or help, that would be great. Thanks

推荐答案

很好地实现你所要求的,你可以把.db文件放在你的资产/目录并将其复制到第一次启动应用程序时就位...

Well to achieve what you're asking, you could put the .db file in your assets/ directory and copy it into place the first time you start your app.....

final String DB_DESTINATION = "/data/data/YOUR_PACKAGE_NAME/databases/MyDatabaseFile.db";

// Check if the database exists before copying
boolean initialiseDatabase = (new File(DB_DESTINATION)).exists();
if (initialiseDatabase == false) {

    // Open the .db file in your assets directory
    InputStream is = getContext().getAssets().open("MyDatabaseFile.db");

    // Copy the database into the destination
    OutputStream os = new FileOutputStream(DB_DESTINATION);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) > 0){
        os.write(buffer, 0, length);
    }
    os.flush();

    os.close();
    is.close();
}

其中initialiseDatabase是指示应用程序是否已启动第一次。

Where "initialiseDatabase" is some flag indicating if the app has been launched for the first time.

虽然,如果你正在寻找存储大量的数据,如你所说:我强烈建议你避免膨胀APK文件,并使用互联网连接在应用程序启动后下载数据库记录(从托管服务器)。这样,您可以显示一个进度条,指示用户为什么他们等待很长时间开始使用应用程序。使APK特别大通常阻止人们安装它在第一位。

Although, if you are looking at storing a lot of data, as you mentioned: I strongly recommend you avoid bloating the APK file with it, and use the Internet connection to download the database records (from a hosted server) after the app has been launched. This way you can show a progress bar indicating to the user why they are waiting a long time to start using the app. Making the APK especially large usually deters people from installing it in the first place.

这篇关于将数据库嵌入到分布式应用程序的.apk中[Android]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆