I want to ship an app with an offline map (and another database) so it can be used without internet connection. The data-db has to be modifiable so I have to copy it to the app's data/data folder no problem with that.. it's < 1MB.
But the Map-DB is about 60MB and is not modified in any way, I don't see the need to put a copy in an modifiable space on the device.
Is there any way that osmdruid can use a DB from Assets (or Raw or anywhere else where I can put it) directly?
Don't know if this is really a osmdroid question, as I didn't find a way to use any DB from assets directly without copy.
If it isn't possible from assets - is there a way to ship it from marketplace other then that?
Messy formulated question, but I believe the answer is no. You have to copy it from assets to SD/local storage.
Related
I am interested in how the mechanics of several note taking apps work. Many of them allow the user to add images and similar to notes. I know that it's possible to save the note's text etc. into a database, but how and where are the images being stored?
What is the most convenient way to achieve this? My guess was that a thumbnail or a compressed file gets created and saved in a certain directory, then the file's path will be stored to the database.
these apps may use external directories, but there is also a way to store data (as bytes, column type named BLOB) directly in database. check out this question-answer for more info
note that storing large size files in database isn't most efficient way.
if you want to know how these apps are storing images root your device and use SQLite debugger (or other way) to look into other apps databases (then you may find above mentioned BLOB columns). or check your device storage (internal and external) for folders with these image files (oftenly apps are creating folders with . in theirs filename, which in Unix based systems mean that folder is hidden)
In my iOS app I have a large (270MB), pre-populated, read-only sqlite database. I keep the data in the app bundle and query it with no problems. I do not copy the database to the user's documents folder, because it would be pointless in this situation to take up more space with a duplicate database. I have a separate much smaller database I copy to the user's documents folder to store the user's preferences. The app works just fine.
Now I'm trying to port my app to Android using Android Studio, but it does not seem possible to access the database from the assets folder. I have found plenty of documentation on database helper classes for Android, which I have tried, but the approach always seems to be to copy the database from the assets folder to the user's data folder. This would be a waste of space and also in my experience the app is unable to copy the database without crashing (maybe because of the size? I had no problems copying a smaller test database).
Is there a way to access the database without copying it to the user's data folder? If not can anyone think of another way of approaching this?
No, You can not directly write into the files of ASSETS folder in your application as the resources folders are read-only.
So You have to compulsory copy your database first from your assets folder to your sdcard and then only you will be able to read & write into it.
As GrIsHu said, you can only read database from asset folder. If you need to do more operation like create, update, delete you can do a tricks. Copy the database from assets folder to storage and then you can do anything you want.
Here is a quick example of Working with Android Pre Built Database.
There is a easy to use library too for accessing database from assets folder. You can check [Android SQLiteAssetHelper] here.2 Good luck!
I am developing an app in android which consists of activities that need to connect to the Database . I added my database file in assets folder which gets copied over to applications database directory on first time the app runs but "assets" directory and "data" directory(on rooted devices) can be accessed by any other application . I'm confusing between using database file or create database in code . If i create database in code it make the database file disappear in the "assets" folder . When users change the file extension from .apk to .zip ,database file will not appear in assest folder . What I should to do ?
Please give me some advice !
Both ways are good and useful it completely depends on your need.
By creating database in code you can secure your data from other applications but it will take so much pain to create it in that way so i suggest you to use a db or sqlite file in assets folder and while copying database on device or data folder use some security parameters to encrypt it or you can hide your app database folder on device so other applications and users will be not able to access it easily.
Well keeping Database in assets folder is not at all a bad practice plus it saves coding of creating a database , as far as you want to make it secure you have to do 2 things
1.keep you database in assests folder , and copy and save it in the internal memory , now its available only to your application and delete it from assests folder .
2.Use Proguard to protect it from somebody decompiling your application and obtaining the assests.
And yea if its a confidential data in the application and your application is worth it then you can also go for "encrypting data" but yea its a TDS task , see for yourself what suits you now.
Honestly, this is the best explained and a very complete tutorial on the subject
SQLite Android Tutorial
Don't get repulsive, because it's a bit longer one. Everything is explained nicely and you don't have to do the thinking about the location of your asset folder on different devices and so on...
I have been gathering information in this site and others about the best way to include a pre-populated sqlite database in Android. I just would like to confirm that I understood the reasons of why something that should be trivial… it is not.
So could someone please tell me if my following conclusions are correct or wrong?:
the main reason many people suggest copying a pre-populated database file from the assets folder to "/data/data/YOUR_PACKAGE/databases/" is because there is no way to access a database file in the assets folder or doing that would be overly complex (?). (Could someone clarify which of these two answers is the right one ?)
another important reason database files in the assets folder must be copied somewhere else is because files in that location cannot be updated. Then even if a database in the assets folder could be open, this would be useful only if such database does not have to be modified.
UPDATE: I launched a new thread focussing only on this issue: Opening a read only database directly in the assets folder.
files in the assets folder can be only 1 Mb size (unless they have certain file extensions such as mp3). Note that this restriction is not relevant if: your database is smaller that 1 Mb, or you do not mind dividing your database in 1 Mb chunks and putting them together at runtime, or you do not mind distributing a database file with a mp3 extension.
If the database file is copied from the assets folder to "/data/data/YOUR_PACKAGE/databases/", there is no way to delete the original database file at the assets folder to avoid having a duplicated file. This is also because files in the assets folder cannot be modified.
Making the puzzle a bit more complex: I found in the comments to the accepted answer of this question: Ship an application with a database
that copying the database from the assets folder to another location in fact does not work on some devices running 2.3+. Is that accurate ? If that is true, then the best alternative would be to download the database file from the web at first run ?
Thanks for any clarification.
You're essentially confusing apks with actual folders on your device.
Think of an apk as an install package - not unlike the msi of the Windows world. The whole goal of this install package is to securely authenticate and deliver code and resources to your device. In a naive implementation, you would then unpack said code to a read-only location, the resources somewhere read-writeable and be on your merry way.
To save space, Android is a bit smarter - the code and resources never leave the signed archive, so you always know it's the ones you put in and you don't waste space by storing the code twice. There's some real magic going on in the class loader that also allows it to unzip classes on the fly but that's besides the point.
So, essentially, everything in this compressed install package is read-only (by virtue of also being signed). It's your job to be the "installer" and move whatever resources you need to a read-writeable location. Of course, you can't touch the apk once it's in place since that would allow for malware and defeat the whole purpose of the signing.
Hope this clears the confusion.
I need a little help with understanding what can I do and cannot in android. I'm working on application which needs to ask user in first start to select a device (internal/external storage) where to save the data which my application is downloading over internet. So I'm trying to find answer for a few questions about this issue :
Is there any limit for data in internal/external storage in Android for a single application.
Can I set the directory of sqlite database which my app is using and If I can, is it a good practice or not.
What should I consider when user decide to change the destination of application's data from internal to external storage. Should I create all the folders and etc. again or Android platform is doing it automatically?
Thanks in advance!
Your limit on external storage(SDCARD) is determined only by its capacity, I'm not sure about the internal storage, maybe it's the same situation.
You can extend SQLiteOpenHelper and create your own DB adapter, which will manage your database and store it on SD card or /data/databases directory. I think that it's better not to place database on SDCARD, because any other app can access it there. On the other hand, /data/ folder is private.
It's tricky thing. You should be careful while copying data. Check whether directory of file exist, before writing into them. It's better to explicitly create all files and directories before trying to write to them.
Here is a good example of Database Helper for accessing database. You can modify it so it can use both external and internal storage.