Reading files in PhoneGap/Cordova 3.1 - android

So, I'm developing an app that requires a prepopulated database. According to Raymon Camden, there's no direct way in packaging a pre-made database with the app. As a solution, I'll execute a Select count(*)... statement to check and insert if result <= 0,
Now problem is the insertion. I have ~500 rows for a table, and I don't want to paste them in the js file. I found the File API and I believe I can read the .sql file, then execute.
Questions:
How does PhoneGap 3.1 (ugh, naming.) package apps? In my www directory I have
js
css
dev
data.sql
So, initially I thought fileSystem.root.getFile('/dev/data.sql', ... would work. (Basing on this, where MattDavis said app data goes to /data/data/packageName.)
How do I access the file?
Am I on the right track? Or, is there a better approach?
I am using Cordova 3.1 to build, with the awesome support of Netbeans.
EDIT
I tried creating a .txt file on the root dir of my device. I can successfully access it with fileSystem.root.getFile('test.txt', ...). And so I infer that I simply need to get the actual app directory.

Yes, your in right track only. Because your access with in asset/www/dev/ inside data.sql.
/data/data/packageName this is for accessing SD inside your app (app internal storage). so no need do like that.

How did I access the file? + Or, is there a better approach?
AJAX. It turns out that I can access the assets folder through:
$.get('file:///android_asset/www/dev/data.sql', function(response){
console.log('Data found!');
console.log(response);
});
I am not sure if this'll work with other platforms.

Related

Cordova/Phonegap - Where do I put a data file I want to read?

I've been driving myself crazy trying to find the answer to a seemingly easy question.
I am trying to create my first app using Cordova. I want to bundle a text file with my app that can be read when the app starts up.
Where do I put this file?
dataDirectory seems like a good place, but where is it? Documentation says /data/data/<app-id>/files but where are those data directories? Do I create them?
Update 1: Ok, I think I've figured out that dataDirectory should be pretty much just where the path says it should be, at the root of the file system. But the directory doesn't appear to be created automatically and I don't know how to package a file - a JSON file, for instance - and place it in that directory.
Essentially you do not need to create these directories. Those are created once your app is installed by the Android itself.
A typical way to get this done would be that you could first off add those files in your app's assets directory and then at runtime you can copy those file wherever you want.
There is a repository on Githup which provide a similar functionality.

Access internal files after built - Cordova

I am wondering if is possible accessing the internal files of an app after build with cordova plugin.
My project is based on Android and here are my files:
index.html
html:
- remove.html
js
css
So, after build the app, I want to access with some option within the app the remove.html and remove it.
I find that the FILE plugin of cordova is useful for files inside the device but not the app.
Please anyone has any idea?
we do not have modification access to the location where apk's internal files are saved. Since your remove.html is part of your app apk, we cannot just remove it.
You can take the same approach taken by sqlite raw database file. When your app is started or run for the very first time, copy your file (remove.html) to data folder where you can modify/delete it. Make sure your app only uses this new path everywhere.
Take a look at copyDatabaseFromAssets function in this code for a sample on how do this. Since you will have modification access, you can delete or modify this file as needed.

Android - Best way to using SQLite?

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...

what kind of file should i use in android to reference information that i stored?

I have created an android app that calculates the numerical values of word, and gives you a list of other words with the same numerical value. The way I have been doing it, is storring the words and value in a .properties file. Ie. A line from a .proprties file called "myWords" will have something like: 61=you, then I just use a get() method to call it,
ie. String myString = ResourseBundle.get("myWords").get("61"); would return the string "you". Is there a better way to do this? My guess is that this is not the proper use of a .properties file, and I was wondering if there was another way to do this correctly. I want to include the file in assets folder of the app, and from my limited understanding of sqlite, you can create a file within android, but you can't just include a file in the assets folder, and then read it. So that said, is there some other type of file that I should use, or was I wrong about sqlite, or is the .properties file being used correctly?
SQLite is your best bet and is the best way to handle your data on an Android phone, that is why Google bundled it on Android in the first place, to avoid people the pain of dealing with files.
If you follow this Tutorial they will show you how to create your database in your computer and then load it up on your "assets" folder and access it from your Android application.
Hope that helps!
You can use a csv file, read it from the assets folder each time the app starts or only once after installation and then store the values in a database.
Take a look at my answer here on how to read the files included in your app (you would use a csv file instead of a libray, but it's still reading files): Hosting an executable within Android application
Edit: here's another example to read from the assets folder: Image uploaded from the android app space seems corrupted
You can try out database option. Here is an interesting tutorial on how to pre-populate a database and then ship it out in the APK.

Best way to do a separate data install on Android

I want to decouple data from code on my Android application, and I am not sure of the best way to do that.
For instance - with the Linux Mahjongg game you can add new tiles to the game by dropping a specially formatted file into a specific directory. The Mahjongg game checks that directory when it starts up.
I want to do the same thing with my Android app - I want to be able to install the app, and then have separate installs for different data files. It's the data file installs that have me hung up. I do not want to have to set up my own server and write my own download code.
You can ship the data with the installer app, then use Input/Output Streams to copy the data from the assets or raw dirs.
Check this out:
Ship an application with a database
The answer has an implementation of in/outputstream. You don't need to use a db, just copy the file to ext storage.
One important detail: if you put the file in assets, it will be shipped compressed, and the phone/tab will try to uncompress the file in its entirety in memory. One (hocky) way to avoid that is to name the file .mp3. Assets in .mp3 format are not compressed. (Hey! I said it was hocky!)
The installer app can either uninstall itself by using ACTION_DELETE in an intent (see http://android.amberfog.com/?p=98 for details) or just show a msg to the user that it's safe to delete the data app.
HTH,
llappall
by dropping a specially formatted file into a specific directory
You can do that on external storage. Create a directory, and check it when your app starts up for new files. Tell the user they have to stick the magic files in the magic directory for it to work.

Categories

Resources