I'm starting an Android project, a port from an existing iPhone project I've completed.
I have a fairly large read-only SQLite database, about 100Mb in all. It's called "mydata.sqlite". Where do I place this in my Eclipse workspace? It's too big for "assets".
Next, how do I best get at the file? I would think to try (handling exceptions later) something like:
SQLiteDatabase myDatabase = null;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
But I would then need the path string myPath and since I don't know where to put the resource I don't know what this needs to be.
Can I put "mydata.sqlite" into "res/raw" (once I create "raw" in Eclipse?) and then reference it as a resource with "R.raw.mydata"?
I would very much appreciate some direct help here, rather than a reference to a tutorial. I have checked tons of these, including those that are already cited here on stackoverflow. I've also gone through the "Notepad" project in the Android developer documents. However these and the documentation typically consider only new, empty or small databases that aren't always persistent. This should be a simple thing and given the time I've spent already it is perhaps easier to ask.
Thanking you kindly in advance for your assistance.
Note that if you are putting a 100MB asset in your .apk, you are going to end up with an app that is too big to publish on Market.
I've never tried to include such a big DB in my app before.
One solution to your question might be renaming your mydata.sqlite to something like mydata.jpg to avoid the file from being compressed by aapt, which after you can put it in asset folder and access it from there.
Any compressed asset file with
an uncompressed size of over 1 MB
cannot be read from the APK.
[source]
Edit: Seems like your best bet now is to retrieve the data in small chunk off a web server when needed? Found this, check out mjc147's solution and Romain Guy from Google explains about the 1mb limitation.
I did it once with a 35MB SQLite database. What I did:
1.) I used a Windows Tool (I think it was TotalCommander) to split that *.db file into pieces of 1MB.
2.) I've put them in the assets folder.
3.) In my database helper class I moved these pieces together during first startup.
It worked perfect and was fast enough.
What you should think about:
1.) At one point you have 3* 50MB during startup on the internal storage so please consider using SD card for your database.
2.) At one point during update of your apps you have 4* 50MB on your device...
Regards
hjw
Related
I have been asked to create a tiny android app.
In everyday work i code for .NET and I have no experience connected with Android, but as it is a really small app I guess it's going to be a good experience rather than something hard.
The core of the app would be a small database (probably XML, unless somebody suggest better solution) that would contain categories, names of the institutions assigned with each category and logo (not very high resolution I guess a single file would be <100kB) of the institution.
The database also would not be very big - I expect not more than 1000 records in total. The DB has to be totally offline and local, it cannot require Internet access when operating.
The model I assume would be to ship new version of the application when the database changes (which is not going to be very frequent).
What is the best way to deal with these requirements?
My first idea was to create an XML file that would contain the records and link to the image. The XML and all the images linked to it would be stored in single file (preferably zip) that would be stored in app resources. This is very good as it is going to be very easy to update the database.
The second idea that somebody suggested me would be to use SQLite and store images in BLOB. In general I have read that it isn't a good idea to store images in database directly, and I am afraid if it's going to be possible to meet all requirements mentioned above.
Mostly I have no idea how to update the database easily and attach it to new version of application.
Any suggestions?
I would be grateful for any response.
I wouldn't go about using XML to save your data and by no means zip anything.
I think your way of thinking is ok, but you're making things really complicated for yourself.
Seeing as you're used to .NET I suppose you're also pretty confident with SQL, so I'd suggest you have a look at how to use the built-in SQLite database in Android.
If you would go the XML route you'd have to serialize and de-serialize the XML file over and over again and then parse the XML. Ok you don't have a lot of data, but searching inside an XML file with at least 1000 nodes would be slow in comparison to the performance of a database.
Also upgrading an existing SQLite database is not that hard - Android has methods for that (onUpgrade coming from the SQLiteOpenHelper).
As to saving images I'm assuming that you won't fetch new pictures from the Internet, so it would be best just to store them in the drawable folder of your app (be mindful of different screensizes) and then reading them into an ImageView when needed. To figure out what image should go for what institution I would store either the image name of each image in the SQLite database or store the resource id for each image in the database - for instance R.drawable.myawesomepictureformyinstitution.
I know my answer is somewhat "superficial", but your question is also somewhat "broad" and hard to answer without me actually writing most of the code, and that's not my intention ;-)
Hope this helps - let me know if anything is unclear.
I've seen the tutorial for <1MB databases, here, which I've run into the same problem that many people posted in the comments (tells me tables that exist don't exist). But apart from that, since it is limited to 1MB, I'm worried that if that DB ends up being larger down the road, I'd have to completely re-work how it is handled.
Which brings me to this question: how can I load a DB from the assets folder in a better manner?
If you are developing android application <3.0 android version,
So the best solutions are,
Shrink your database file size and make it smaller then 1 MB.
Or just describe table structure in database file and download larger data from web server (using web service).
Cut your database file in separate chunks, Load files bigger than 1M from assets folder
But there are some quick fixing solutions are also available,
I seen one comment in SO for same kind of question which just change the SQLite db file extension and use it,
It something changing of extension of the file in assets folder from .sqlite to ".jpg".
(Try this if its worked in your case, As I never try it)
Also look at this post about Dealing with Asset Compression in Android Apps
you will get something helpful from that.
Hello everyone first of all.
I am making an application for android, which I need to have a database for information.
The database management computer mailbox.
What I need is to take that database and included in the application for android'S SELECT generally.
I've been testing it on this website, but I can not get it to work. I do not think either the DB copy to memory, and then to perform the query, she says she can not find the table.
I have been debugged and saw that even the open works, but something strange happens ...
If you have some method or way of doing this, I would really appreciate it.
Thanks for everything.
Check out these resources
This tutorial by Lars Vogel
This reference article from Android website
Puit it in your assets directory in your apk, and on first use copy to "/data/data/YOUR_PACKAGE/databases/" directory. Here is SO discussion on this topic. database in assets folder
I'm making an dictionary app for android. This app uses stardict and DICT files which is are often pretty large (10MB or more) and contains just plain texts.
When users look up a word, my app will read the file randomly and return the result. I've read on some articles that reading file is expensive and slow. So I wonder if bringing all data into database is a better solution?
I would suggest putting your words into a database for the following reasons:
DB lookups on android with SQLite are "fast enough" (~1ms) for even the most impatient of users
Reading large files into memory is a dangerous practice in memory-limited environments such as android.
Trying to read entries out of a file "in place" rather than "in memory" is effectively trying to solve all the problems that SQLite already solves for you.
The only challenge presented in using a database is initializing it with the data you require. This problem can best be solved by creating the desired database in advance and then attaching it to your APK assets. There is an example here that is quite good.
Hope this helps.
I am trying to build dictionary app(actually it is a modification of google SearchableDictionary sample), whose source of words and their definitions is very big, around 5MB. I tried many ways and using many formats and it still cant run properly on android. Sqlite database should be the best solution, I have built it and its size is 10MB(tried building it both before runtime and during runtime).
The main problem is the size of the definitions, but I have seen some other applications have managed to do this. It might be that there are some file size limits built into android system, but anyways if they werent it all takes so much to search and run queries in this sqlite database.
What am I doing wrong?
BTW: It HAS to be offline dictionary (download definitions max 1 time).
Problem in a nutshell:
word -definition
word2 -definition2...
Stored in a 10MB sqlite database (tried loading it from assets), not working.
With some hacks (loading it manually with eclipse DDMS tool) it is working but terribly slow.
Are you loading the database from the Assets folder? If yes, then that's your problem. There is a file size limit on what is in the assets folder (1mb I believe).
You have two options:
Split up your database into multiple 1mb files
Create a webservice. Have your application call the webservice which in turn downloads the database to your Android device. OR create a webservice API that your application uses to get data on as it needs it basis.
I have achieved by zipping the database and unzipping it into external caching direcotry (SD Card). you can look at the sample code here - http://www.android.manutech.mobi/2011/03/how-to-manage-sqlite-databases-with.html
Rename it to something like "databasename.mp3" or any media format. It won't be compressed by the package manager and therefore you can use it just like you need.
Have you tried compressing the file? If the data is just raw text I bet it'll compress to smaller than 1 MB.
1) Store a compressed version of only the words using the GZIP. (this will be very small only about 350k, and must be pre-sorted)
2) Load the list into a:
new ArrayList<String>();
3) Use a binarySearch to find the word
.binarySearch
4) When you need a definition, call an API like this with the word
http://services.aonaware.com//DictService/DictService.asmx/Define?word=aardvark
5) parse the resulting XML
10M file is just way too small to worry about. It didn't work, it might be because of the way of opening the database file. By default, Android read database file from ://data/data/PACKAGENAME/databases/DATABASENAME.
A simple solution could be: 1) compress the db file to res/raw/DATABASE.zip, 2) then unzip it to //data/data/PACKAGENAME/databases/.
You can get sqlite android demo code from here: http://www.cs.dartmouth.edu/~campbell/cs65/lecture15/lecture15.html