Best way to store multiple users's data in android application - android

I'm quite new to android and I'm trying to build an application where userss can create multiple settings profile and store them in the internal storage. Sinse the data being stored are settings, I was thincking to store them in xml file and creating a different xml file for each profile.
In example:
profile1.xml
profile2.xml
etc...
Is it a good idea?
How can I create those xml files in the internal memory and write each setting in them?
I've seen something like
String FILENAME = "profile1";
String setting1 = "set";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(setting1.getBytes());
fos.close();
but how can I specify that the file created is in xml format? And how can I create the setting corresponding tags(to parse them later by id for example)?
Thank you and sorry for my bad english.
Alessandro

You can use shared preferences, its format is xml, its quite easy to access it, modify/read and should be quite straightforward to implement.
Here is an example that might help you http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
Here are storage options explained http://developer.android.com/guide/topics/data/data-storage.html

Related

Getting /data/data/my.app/files path programmatically?

In my app, I've just successfully downloaded a file and I opened it like this:
OutputStream output = openFileOutput("myfile.txt", Context.MODE_PRIVATE);
With a little browsing, I've found my file in /data/data/my.app.namespace/files/myfile.txt. I don't want to hardcode the path but later on in my app I'm going to want to access myfile.txt and I want to dynamically get that folder path. I've tried most of the folders in the Environment object like Environment.getDataDirectory().getAbsolutePath() but that only returns /data. Where can I look to get where private files go? Or is it safe to assume that this structure won't change? I don't like hardcoding it, but it would work...
you could do it by using context.getFilesDir() + "/filename.ext"

Android: Writing to XML file in Assets

I am really lost here, i am trying to find my way around xml parsing, reading and writing.
I have this app where at one point i can input data such as a Date and a time for instance - click save, and once it saved it will write into an existing XML file, for later reading, and add it at the end in a format like this:
<Units>
<item>
<date>27-5-12</date>
<time>15:30</time>
</item>
<item>
... and so on ...
</Units>
i managed to read an xml file, but i am really having trouble in opening a premade - existing file for reading or writing.
currently i tried this code:
InputStream raw = this.getAssets().open("mydata.xml");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
which returns file not found exception.
could anyone direct me on what i should look for?
Thanks.
As written, your source XML file is located in your APK's assets directory - everything in your APK is read-only, so you won't be able to write to that file. (Also, you should probably put that XML data into the res/xml directory instead of the assets directory, unless you have a compelling reason to do otherwise.)
If the XML file isn't very long/complex, you could read the assets file into a structure, then add your new data to that structure, and write a new XML file into your app's data directory with the updated data. This approach has the advantage that you can have multiple source files feeding into one main file per app.
A more flexible and open-ended option would be to set up a database table. When the app is first installed, you load/update the table with data from the assets file. As your app keeps adding timestamped data, you just add new rows to the table. This approach also has the advantage that you can easily update the source data or the database structure with each app update - it's harder to compare old vs new data if it's stored internally in XML format.
I didn't see the assets folder in my project, i placed my xml file there and it works now :)

CSV File Import/Reading - Android

Pretty short question:
I have put a .csv file in my assets folder. My problem is that I'm unsure how to read it and store its contents in an array-like form that I can pass to a function that then loops through the contents.
So, basically I want something like (pseudo code):
VariableArray var[] = new Variable (get the file contents);
performFunction(var[]);
How I can accomplish this?
You can use this to get an InputStream to your file in assets.
InputStream is = getAssets().open(fileName);
Then you just need to make a reader and loop through the file until the end of it.
I suggest you to use the JavaCSV library to deal with CSV formats. There are a lot of things it can accomplish in very few lines of code.

Sorting Images on Android

I need images to be sorted by folders. like drawable/Photos, drawable/items, drawable/photos. and put each folder to the registry or at least find way to get them out of there. all I want is something close to php like "../img/photos/photo0.jpg". is there way to do something like this. and folder with Images must contain in apk file.
possible solutions is make link in the R file but I didn't find how to do it, other solution is find command with logic like I will show you here:
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageDrawable(Drawable.createFromPath(ImgPath));
OR
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageResource(ImgPath);
please say me the best way to handle it. AND specify if it contain path how I can know path the file lies in.
File ImgFile = new File("test/test.jpg");
TextView testtext = (TextView)CloningTable.findViewById(R.id.testtext);
if (ImgFile.exists()) {
test.setImageBitmap(BitmapFactory.decodeFile(ImgFile.getPath()));
testtext.setText("asdasuidjasdk");
}
can any one say Why programm can't find file and file exist 100% = /?
filepath: Project> assets/test/test.jpg
found solution: Android Show image by path
It's hard to understand why you need to sort resources that will be static in your APK, but you should not access them directly using paths, but using the API for that purpose.
Take a look at the topics in the dev guide here:
http://developer.android.com/guide/topics/resources/accessing-resources.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
Those will teach you how to access the resources. To create Bitmaps from them, the easiest way is to use the BitmapFactory class
Remember, you know which resources the APK has at building time, so you can work around it. If you want to work with Bitmaps created at runtime, then use the data storage methods instead.
In this case, you should put resources in assets folder, and you can access them by path!
So if you want to save the path string, the best way is creating a class, with 2 variants: 1 Bitmap to store data, and 1 String to store the path.
If not, just create a String array that you may access when you want.
In both case, you need to put the path into the Path String storage before you load the image.
Hope I understood your question.
EDIT: Final answer that meet your requirement:
There's no path for drawable, they are all converted into ID. If you put in asset folder, just call it by their name. For example, you have "Test.bmp" in asset folder, its path is "Test.bmp". If you have "SubTest.bmp" in "TestFolder" in asset folder, its path is "TestFolder/SubTest.bmp". Sorry for long time, I had to sleep, it was mid night at my time zone.

How can I create an external shared pref/config file for my Android application?

I am looking for a way to store configuration setting on an external file on the sd card. The app must look into this external file and then retrieve some kind of settings. At the moment i am trying to get it to look into a file and get a name. I know you can store things in shared preferences but they are internally accessed.
Anybody know an external way? Thanks
The idea is to have a simple text file or xml file on the sd card. So when a configuration needs changing it is done thru that file?
EDIT
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"/Config.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line="";
int c;
while ((c = br.read()) != -1) {
line+=(char)c;
if(String.valueOf(c) == ";" & line =="name;"){
String name ="";
}
}
}
I have attempted to read in a config.txt file and to separate each value with a ;
But i cant seem to understand exactly how i am going to attach what comes after name; to the variable String name.
the config.txt file has the following:
name;fred
somethingelse;test
The program should know when it has got to name and then set the name variable to fred??
Hi
I had same kind of preferences reading in Blackberry with (Comma separated or) semicolon separated values. When same development came to android the developers (I was not on android at that time.) This is what they have done.
We had created a text file like this
"name";"value";"data_type"
For example
"application_runs";"1";"int"
Or
"trial_period_key";"01ab23cd";"string"
The data type is hardcoded so we can detect the datatypes. For variable names we also had hardcoded list of preferences. And values can be parsed accordingly. They have written public readable shared preference based on that txt files, to use default values, they have copied a "default.txt" in assets folder along with a copy in SD card.
The drawback of this procedure is
You have to be careful about file name and the data written in that file (this is the reason why we had put a default values file in assets so the app doesn't crash)
The text file on SD Card must be readable, you have to program it along with parsing.
Hope it helps.

Categories

Resources