How to store initial data in android apk file? - android

I'm writing an android weather app, which needs a city information list.
The list is quite long, and spends long time to be downloaded from internet.
So I want to save it in my .apk file, and load it without internet connection, but don't know how.
I tried to save it in a java class, but it's toooo huge and costs much memory.
Are there some ways to solve it? Thx!

did you look at res/assets?
I assume that you probably have some sort of json/xml data that you want to pre-package with your app if so, then load the json/xml into the res/assets and it'll just be a file included with your apk.
Ref
https://developer.android.com/guide/topics/resources/accessing-resources.html

You can store it in SQLite database or use plain text then save it in raw or asset resource in your app project.
For SQLite, try Android SQLiteAssetHelper which will help you with storing data from a predefined database.
Here an excerpt from its README:
An Android helper class to manage database creation and version
management using an application's raw asset files.
This class provides developers with a simple way to ship their Android
app with an existing SQLite database (which may be pre-populated with
data) and to manage its initial creation and any upgrades required
with subsequent version releases.
It is implemented as an extension to SQLiteOpenHelper, providing an
efficient way for ContentProvider implementations to defer opening and
upgrading the database until first use.
Rather than implementing the onCreate() and onUpgrade() methods to
execute a bunch of SQL statements, developers simply include
appropriately named file assets in their project's assets directory.
These will include the initial SQLite database file for creation and
optionally any SQL upgrade scripts.

you may use arrays from xml resources. For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="cities">
<item>New York</item>
<item>London</item>
<item>Moscow</item>
....
</string-array>
</resources>

Related

Which approach is better creating database programmatically or copying it from assets folder?

Since there are two ways by which we can store or copy the data from database. one is from sqlite file which we lay in assets folder and another by database which is created programmatically.
I have following questions regarding the sqlite database in android:
1.which performs faster while getting performing select query from database and why?
2.what are the advantages of each approach.
I found on internet but i could not get any proper information, the only thing that i come to know by Googling is copying data from assets is better for static data like list of country(that is obvious because you will not perform any bulk operation of inserting data programmatcially in android). Any help regarding this is appreciated . :)
It does not make a difference.
Your select query is performed on the final database, which would be identical whether you copied it from assets, or imported it programmatically. Your approach to its creation does not influence its behaviour.
As for advantages, that's entirely up to you. In some scenarios, having a base database in assets would give you better, and easier to read, control over versioning, as you could commit it easily with your repository, vs. having thousands of SQL statements in code.
On the flip side, updating a database that you copy from the assets folder with each version could be difficult, especially if you modify data within the app frequently. Instead of programmatically simply altering the schema or data on update, you would have to export any modifications, copy the new database, and then import all your modifications by performing some form of merge.

How to insert a database into an Android application

I have created a database which contains 4 tables using Firefox plugin. The format of the saved database is .sqlite(name.sqlite).
I just want to know how/where to insert it into the android project that I am developing and what the changes are that I should make in my project (like changes in the Manifest file). I guess I should paste it into an asset folder, but I do not know how to access it in the code.
I want to add, view, update and delete data in the table, how should Ido this? Or Should I use another way?
This is a pretty solid tutorial: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Basically, don't copy your sqlite file directly into the project. Follow the steps and create / upgrade your database in the provided onCreate and onUpgrade hooks.
These hooks give you the opportunity to change your database structure when publishing updates through the Play Store.
And the proper places to do longer running operations to upgrade all your data, or prefill your data from disk/network.
If you want to prefill your DB from a dataset, you can provide that data in a raw XML, or JSON, or CSV, and after your onCreate do all the inserts when the app launches from a fresh install.
Here is some good tuts for sqlite...
http://coderzheaven.com/2011/04/using-sqlite-in-android-a-really-simple-example/
http://androidexample.com/SQLite_Database_Manipulation_Class_-_Android_Example/index.php?view=article_discription&aid=51
http://www.vogella.de/articles/AndroidSQLite/article.html

Android App: can I use iOS sqlite? If not, how do I create a local database from a database in the cloud (Parse.com)?

I am making an Android version of an iOS app that I already have. I want to use a local database so that the user can use most the app offline.
Initally I thought I could use the sqlite database created by Core Data in Xcode, but as I am reading things online it seems like this is not possible. Is this true? Or is there a good way to export it to something Android could use?
If not, I want to create a local database with values from a database on the cloud(I use Parse.com). How can I do this? The data on the cloud doesn't change very often (maybe twice or thrice a year) if that makes any difference.
This is a good tutorial to handle preloaded databases:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Essentially, once you have your precreated database, put it in your assets directory in your apk. Then on first app use, copy this from assets to "/data/data/YOUR_PACKAGE/databases/" directory.
You have to create the database by code in Android, using a DatabaseHelper http://developer.android.com/guide/topics/data/data-storage.html#db. If you want to use your already created database you will have to export it to SQL statements and then "import" them to your application.

Android: is it safe to use a preloaded database file?

I have a huge set of data that I want to insert into the sqlite database before the user is able to do anything inside my application. Right now I have all my data stored in CSV files, then I parse those files and use DatabaseUtils.InsertHelper to do a bulk insertion, but this is taking to long to complete.
I stumbled on this tutorial some time ago and I'm wondering: is it safe to distribute a pre-generated sqlite file? Can I run into problems due to different versions of SQLite on different devices?
I'm planning to support Android 2.1 and higher.
I suppose it depends on your definition of safe. It is certainly possible as long as the database conforms to the metadata table spec Android expects, which is what that tutorial you stumbled upon is showing you. You won't have to worry about version conflicts with SQLite as that is a package built into the core platform and isn't something OEMs add to or implement anything on top of.
However, if by safe you mean "protected" you would need to take special steps to ensure that your database is not externally readable if that is a concern. If you simply place the preconstructed DB into assets/ and copy it over, anyone who can properly deconstruct an APK file can view your database data. This may or may not be an issue for you.
The best approach is to populate this data in the database, keep the database in assets & then copy it to the device ... You can follow this complete sample code here.

Using an independant database in android apps

Basically, I'm trying to store some data (~300 rows, ~10 columns) for an android app. This data will not be changed by the app, just used in calculations and stuff.
However, everything I've found online (example) talks about using a database that is created at runtime. I do not want this, the data will be the same every time the app is run.
So is there a way of using a databse like this? (Or any other way of storing data in a table-like fashion?)
Generate the SQLite database as part of your build and keep it in your app's raw resources. Since all you need is the file's path and name to open it, you can still read it fine. Your open helper will still go through onCreate() the first time unless you include the table Android uses for its own bookkeeping, but that should be okay.
Make sure you only open it for reading, and you should be good to go.
Put your custom file in the assets folder under the project root.
To get a inputstream from the file, just do:
context.getAssets().open(file);
In this way you can store your static data in conma separated or any model you want.
If you want the data constantly changing, you can create a temporary file in the SDCard, by accessing and creating a new file under some path at:
Environment.getExternalStorageDirectory()
How about storing this data in raw file under Assets or Res/raw folder. You can either dump this data on the fly in Database or read it and process it [which may be costly]. Dynamic handling may be costly, test it and compare performance.

Categories

Resources