File folder on Eclipse Android - android

I would like to make a xml file that will be modified during the execution of the application and i want to keep it after i close it for the next time i open it.
The first problem is that i don't know where do i have to put the file in the package explorer on Eclipse.
If i put the file on res/raw/ folder i could just read the file, but i can't modify.
I'm working with Jdom2.
The file is a score table for a game that will be modified every time the player finish a game.
That's the code i actually have to read the xml file stored on res/raw
try
{
puntf = getResources().openRawResource(R.raw.punt);
} catch (Exception ex)
{
Log.e("Ficheros", "Error");
}
And that's the code i actually have to modify the xml file(with Jdom2). But of course, that is wrong.
public void escritura()
{
try
{
xmlOutput.output(puntu, new FileOutputStream("punt.xml"));
}
catch (Exception e) {
e.printStackTrace();
}
}
Thanks a lot for your answers.

If you want to save a file and modify it programmatically I would suggest you to store it in this path:
/data/data/com.yourpackage.app/punt.xml
I have never worked with Jdom2, but you can have access to it by adding these lines of code:
File puntFile= new File("data/data/com.yourpackage.app/punt.xml");
FileOutputStream fos = new FileOutputStream(puntFile);
xmlOutput.output(puntu, fos);
You can also see the file in DDMS in file explorer. Just follow that path.
I can't understand if you want to save and edit the file during the process of your application or just save it somewhere before the app starts and edit it afterwords. If so, please give more details about that.
Hope I helped...

You should read uo on storage options on Andriod: THere's an article on this.... Use the resulting input and output streams for JDOM.

Related

Android, What is the right way to write a file to a subdirectory of the data/data/'package' folder

I have been trying for the last couple of days to find the right way of writing a file to a subdirectory of the android data folder.
Most answers i found were not clear or didn't address the problem in a right and working way so i finally decided to ask.
I am giving a user the possibility of backing up his data on my server by basically uploading the database to the server through a php script.
The database is located in
'/data/data/com.package/databases/data.db'
The problem here is when a user wants to reload a backed up database.
The application Downloads the file and should then write it (Overwriting the old one) to the /databases/data.db' file, and then reload.
I have managed to get everything to work up until where i have to write the downloaded file because FileOutputStream throws an illegal argument exception saying that i can't use path separators in the path.
I understand that FileOutputStream can only write to the first level of the data folder and not to subdirectories.
How can this be done?
If it can't be done, is there any way to set the default database path to the first level of the 'data' directory to solve this problem?
If this is a completely wrong approach to what i want to obtain i am open to critique but it would still be nice to get an answer, just for future knowing.
public void aSyncDatabaseDownloadFileFinish(PhpWrapper feed) {
if (validateServerResponse(feed.Result)) {
// Copy Database to Directory
try {
FileOutputStream fos = ctx.openFileOutput(ctx.getDatabasePath(DataDatabaseHelper.DBNAME).getAbsolutePath(),
Context.MODE_PRIVATE);
fos.write(feed.DownloadedBytes, 0, feed.DownloadedBytes.length);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else
Toast.makeText(ctx, ctx.getResources().getString(R.string.nofilesonserver), Toast.LENGTH_SHORT).show();
}
Thanks in advance.
How can this be done?
In your case, use getDatabasePath() to write a file into the standard location for databases for your app.
Regardless of your path separator issue, openFileOutput() will work with files/ directory in your internal storage, not the databases/ directory.
Ok,
I got the answer thanks to Squonk, Still can't believe that it was that simple.
For anybody having the same problem here is an example:
if (validateServerResponse(feed.Result)) {
// Copy Database to Directory
try {
// This Solved the Exception
FileOutputStream fos = new FileOutputStream(ctx.getDatabasePath(DataDatabaseHelper.DBNAME), true);
fos.write(feed.DownloadedBytes, 0, feed.DownloadedBytes.length);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else
Toast.makeText(ctx, ctx.getResources().getString(R.string.nofilesonserver), Toast.LENGTH_SHORT).show();
Thank you Squonk!

Robotium - Write to file in eclipse workspace or computer file system

I'm running some tests using Robotium on an Android application that interacts with a web-portal.
I'd like to save some information to file; for example I need to save the id of the username I created from the app and I want to make it read from Selenium to run tests on web-portal to verify a webpage for that user has been created.
Is it possible?
Could someone suggest me a solution or a work-around?
This is an example of code, but it doesn't work (I want to write to a file for example on c:\myworkspace\filename.txt a string):
public void test_write_file(){
if(!solo.searchText("HOME")){
signIn("39777555333", VALID_PASSWORD);
}
try {
String content = "This is the content to write into file";
File file = new File("filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertTrue(solo.searchText("HOME"));
}
This code should write to file on device; my goal is to write on a file on machine from which I'm launching the script; the application under test should have permission to write to memory card; but I ask how to go out from Android Environment and get my Desktop environment.
For tests I suppose you will need xml format to be saved: Create xml file and save it in internal storage android
And then you will need to copy saved file from your device, see this How to copy selected files from Android with adb pull
You could be not so lazy and search it yourself.
For reading from a file or writing to file you would have to use normal java method. There you can create a separate method to read/write, which can be called whenever needed. you can see examples here for normal text file and excel file.

Check if a file has been written to in Android

I have a question about Android programming. Basically, I am unsure of where to check where my file is, and if I wrote to it correctly. I want to locate where the file is, and I also want to know whether or not I wrote to it correctly. Below is the code I have come up with:
String lsNow = "testing";
try {
fos = openFileOutput("output.txt", Context.MODE_APPEND);
fos.write(lsNow.getBytes());
fos.close();
}
catch{
...
}
Where can I find output.txt? Might anyone know how to check this all out? if so, that would be great! I am using an emulator by the way. If I were to do this on a real Android, how would one approach this also? (Just for future reference)
You Test it in Two ways
Using File Explorer
Go to DDMS perspective--> Open File Explorer-->location of the file
Pragrammatically by using exits() method
File file = new File(context.getFilesDir(), filename);
if(file.exists())
Using openFileOutput(...) means the file will be written to internal storage on the Android device in an area which is secure from access by other apps.
If you want to make sure the file is written correctly then make sure your catch block handles any failures (if it is called then the file writing has failed).
To access the file once it has been written use openFileInput(...).

creating a xml in android

I want to create a new xml file in android.my code is like below.It didn't work
File newxmlfile = new File("C:/Users/yunus.oksuz/Desktop/xmlFile.xml");
try
{
newxmlfile.createNewFile();
Toast msg = Toast.makeText(MainActivity.this, "file was created", Toast.LENGTH_LONG);
msg.show();
}
catch(IOException e)
{
Log.e("IOException", "exception in createNewFile() method");
}
First thing to know, you cannot even think about having a file in your PC and trying to refer it to your Android application project. They both are totally different.
let me explain the reason.
Consider you have the app installed in a phone, how do you think you can simply access your PC to which you are no more connected. This is not a regular java project you are working on. Android is totally different.
I will tell how it works. Open a emulator and wait for it to load. now,
Go to DDMs-> File Explorer
You will be able to see a folder by name "mnt" or "sdcard". Click on it. And now on the top right corner you will have three icons , one to push a file to sdcard and one to delete from sdcard and also one to pull a file from sdcard.
So click on the push a file icon and select your file and copy it to sdcard.
Now you have your file in sdcard. here you have to learn how to read a file from sdcard.
And I am sure there are so many examples available on internet on how you can read a file from sdcard.
Hope this will help you to get it started.
Here's how you can create your xml file.
File f = new File(Environment.getExternalStorageDirectory() + "/yourxmlfilename.xml");
// If file does not exist, clear log count
if(!f.exists())
f.createNewFile();

Saving XML files to a Running Android Project

First: see my question Reading XML online and Storing It (Using Java). Read the approved answers and the comments underneath that answer.
So, what my question here is: even though I've run through the process described in the linked question, and the .xml file saves to the /res/values folder in my Android App, its not showing up at all - not when I'm running the app, nor after I close the app.
Does anyone have any ideas on how to fix this so that when I generate the file, it will be available right away, even while the app is running, to read and use?
just use this code,
FileOutputStream fOut = null;
try {
fOut = this.openFileOutput("your xml file name.xml", MODE_PRIVATE);
try {
InputStream is = new FileInputStream("your source file");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
fOut.write(buffer);
} catch(Exception e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
and you can see your xml file at the data/data/packagename/file folder Thnx. Hope this will help you.
I'm not 100% sure if you're running the XML parsing in Java or actually in your Android app.
If you're running in Java, be aware that your project structure isn't live in the emulator - the .apk was packaged up and installed before running. You need to use adb to push files into the emulator (or your Android device) before your app can see the file.
If you're accessing the file in the app:
If you use file access methods such as openFileOutput() it will show up in the private directory on the device, which would be /data/data//files/
However, if you're using "new File(" rather than "context.openFileOutput" then the file is wherever you put it.

Categories

Resources