| Author |
Message |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2660 Location: College Park, MD
|
Posted: Tue Nov 27, 2007 12:45 am Post subject: Working with Files |
|
|
Working with Files
What you learn: You will learn how to access file (read and write) and why we cannot use standard Java file-access.
Problems/Questions: Write them right below...
Difficulty: 1 of 5
What it will look like:
 This is the first tutorial without a screenshot 
Description:
To understand why we cannot use standard java file access, like:
| Java: | FileWriter f = new FileWriter("impossible.txt");
// throws: 'java.io.FileNotFoundException: /impossible.txt ' |
, we have to understand the Security-Model of Android.
Each *.apk File that is installed on the Emulator/Device gets its own User-ID from the Linux System. This ID is the key to the sandbox of the application. This 'sandbox' protects the application (and its files) from other bad apps, that i.e. want to manipulate the files we created in a bad manner (Like writing into them: "Whos reads this is... dumb ! Hhahaha").
| Note wrote: | But, writing to SD-Cards is still possible with 'normal' Java methods
| Java: | FileWriter f = new FileWriter("/sdcard/download/possible.txt"); |
will work without any problem, you just need to add virtual sdcard to your emulator. |
Every file that you generate with your code gets is 'signed' with the User-ID of the Application that created it. One From within your app you can set flags to make the file accessible (read and/or write) for other applications with other User-IDs (Flags: MODE_WORLD_READABLE and/or MODE_WORLD_WRITEABLE).
There is a way to make two applications use the same userid (look here).
1.Writing a file...
| Java: | try { // catches IOException below
final String TESTSTRING = new String("Hello Android");
// ##### Write a file to the disk #####
/* We have to use the openFileOutput()-method
* the ActivityContext provides, to
* protect your file from others and
* This is done for security-reasons.
* We chose MODE_WORLD_READABLE, because
* we have nothing to hide in our file */
FileOutputStream fOut = openFileOutput("samplefile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(TESTSTRING);
/* ensure that everything is
* really written out and close */
osw.flush();
osw.close(); |
2.Reading the file back...
| Java: | // ##### Read the file back in #####
/* We have to use the openFileInput()-method
* the ActivityContext provides.
* Again for security reasons with
* openFileInput(...) */
FileInputStream fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
/* Prepare a char-Array that will
* hold the chars we read back in. */
char[] inputBuffer = new char[TESTSTRING.length()];
// Fill the Buffer with data from the file
isr.read(inputBuffer);
// Transform the chars to a String
String readString = new String(inputBuffer);
// Check if we read back the same chars that we had written out
boolean isTheSame = TESTSTRING.equals(readString);
// WOHOO lets Celebrate =)
Log.i("File Reading stuff", "success = " + isTheSame);
} catch (IOException ioe) {
ioe.printStackTrace();
} |
The file is now located in the following folder on your emulator:
"/data/data/your_project_package_structure/files/samplefile.txt"
How to browse the FileSystem of the Emulator watch 3 posts below.
You delete a file with the following method coming from ApplicationContext:
| Java: | public boolean deleteFile(String name)
// Delete the given private file associated with this Context's application package. |
Regards,
plusminus _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials
Last edited by plusminus on Mon Dec 17, 2007 4:34 pm; edited 5 times in total |
|
| Back to top |
|
 |
|
|
 |
fresco Junior Developer

Joined: 18 Nov 2007 Posts: 24
|
Posted: Tue Nov 27, 2007 4:56 pm Post subject: |
|
|
You forgot to add that we can use the regular java methods like you mentioned "FileWriter f = new FileWriter("impossible.txt");" when you workind with sdcard.
| Java: | FileWriter f = new FileWriter("/sdcard/download/impossible.txt"); | will work without any problem, you just need to add virtual sdcard to your emulator. |
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2660 Location: College Park, MD
|
Posted: Tue Nov 27, 2007 6:08 pm Post subject: |
|
|
Thanks fresco,
added it above
Regards,
plusminus _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials |
|
| Back to top |
|
 |
rtheman Freshman

Joined: 28 Nov 2007 Posts: 4
|
Posted: Fri Nov 30, 2007 10:56 pm Post subject: where's the log written at? |
|
|
| So where can I find and view the log entry? Better yet, where is the .txt file that's written? I'm doing this on a XP box |
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2660 Location: College Park, MD
|
|
| Back to top |
|
 |
Gamby Freshman

Joined: 26 Nov 2007 Posts: 7 Location: Italy
|
Posted: Wed Dec 12, 2007 11:28 pm Post subject: Other file operations? |
|
|
Hi, thanks for the tutorial.
But, what about deleting and asking the existence of a file? How should I perform that operations?
I tried in this way:
| Java: |
if (files[i].equals("samplefile.txt")) {
File f = new File("files", files[i]);
if (f.exists()) {
Log.v(TAG, "File exists! " + f.getAbsolutePath());
}
else {
Log.v(TAG, "File NOT exists! " + f.getAbsolutePath());
}
if (f.delete()) {
Log.v(TAG, "File deleted!");
}
else {
Log.v(TAG, "File NOT deleted! " + f.getAbsolutePath());
}
}
|
But the Log says:
File NOT exists! /files/samplefile.txt
File NOT deleted! /files/samplefile.txt
If i go in adb shell or in file explorer I can see that file located inside files folder
And what about "files" folder? Is it a standard unchangeble folder?
Should I put my files inside that folder if I want my app is able to load them?
Thanks and best regards!
Gamby |
|
| Back to top |
|
 |
|
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2660 Location: College Park, MD
|
|
| Back to top |
|
 |
Gamby Freshman

Joined: 26 Nov 2007 Posts: 7 Location: Italy
|
Posted: Wed Dec 12, 2007 11:41 pm Post subject: |
|
|
Thank you very much plusminus!
And what about these other quqestions, can you help me to understand?
| Quote: | And what about "files" folder? Is it a standard unchangeble folder?
Should I put my files inside that folder if I want my app is able to load them?
|
Thanks again best regard.
Gamby |
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2660 Location: College Park, MD
|
Posted: Wed Dec 12, 2007 11:47 pm Post subject: |
|
|
should have had a closer look ^^
it depends on how you have created the file
Creating a file with the procedures Android provides "marks" the files to the "User-ID" of the specific *.apk-File (Application).
When you create a file with MODE_WORLD_WRITEABLE it will be writable ( probably also readable ) by other Applications. MODE_PRIVATE will disallow them to do so.
Regards,
plusminus _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials |
|
| Back to top |
|
 |
Gamby Freshman

Joined: 26 Nov 2007 Posts: 7 Location: Italy
|
Posted: Wed Dec 12, 2007 11:56 pm Post subject: |
|
|
That file i'm talking about, I haven't created it from code, but I have pushed it with "adb push".
A pushed file, I think, it is a private file for the application(MODE_PRIVATE)
Reapting with other words, m question is:
If I want my application was able to read the files that I push inside (for example) /data/data/my/package/ahyeah_folder
which apis should I use to read them?
Downloaded files from web do they finish? in which folder by default?
Thanks, regards!
Gamby |
|
| Back to top |
|
 |
rtheman Freshman

Joined: 28 Nov 2007 Posts: 4
|
Posted: Thu Dec 20, 2007 3:54 pm Post subject: Thanks - more example for pulling in data? |
|
|
Thanks for the 'extra' tutorials to locate such file...
I haven't been able to look though this whole site yet, but what I'd like to do is be able to pull in files with data (encoded in XML) such as weather, stock, other custom data; then display it on android such as weather front on google map rendering Rain, Snow, Wind, temp data on the xml file with some sort of graphics on android using g-map.
Is there something similar to this exist as tutorial? I'm really a newbie w/ Java and step by step is what I need... Thanks!
| plusminus wrote: | Hello rtehman,
This way to get to the DDMS-Perspective in Eclipse (if A isn't visible, do B):
On the Bottom you will see the so called "LOGCAT"-View appear where the Logs are written to.
Here switch to the File Explorer.

The file is now located in the following folder on your emulator: "/data/data/your_project_package_structure/files/samplefile.txt"
(added it above)
Regards,
plusminus |
|
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2660 Location: College Park, MD
|
Posted: Thu Dec 20, 2007 4:34 pm Post subject: |
|
|
Hello rtheman,
we already have:
Let us know if your are still missing sth.
Regards,
plusminus _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials
Last edited by plusminus on Thu Dec 20, 2007 11:43 pm; edited 1 time in total |
|
| Back to top |
|
 |
rtheman Freshman

Joined: 28 Nov 2007 Posts: 4
|
Posted: Thu Dec 20, 2007 11:23 pm Post subject: error and can't parse from XML |
|
|
Thanks "+ -",
Sorry if this is a dumb question, but I'm getting the following error, "ctx cannot be resolved"
Lastly, can you direct me or a sample parse statement? I tried following the code in http://www.anddev.org/viewtopic.php?p=1091#1091, but no luck
Thanks and hope you can shed some light for this newbie.
Code for Import_XML.java
| Java: |
package caasd.mitre.Import_XML;
import org.xmlpull.v1.XmlPullParser;
import android.app.Activity;
import android.os.Bundle;
public class Import_XML extends Activity {
// Called when the activity is first created.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// Get XML
XmlPullParser xpp = ctx.getResource().getXml(R.xml.sample);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
// parse stuff
}
else if (eventType == XmlPullParser.END_TAG)
{
// parse stuff
}
eventType = xpp.next();
}
}
}
|
|
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2660 Location: College Park, MD
|
|
| Back to top |
|
 |
igal Freshman

Joined: 16 Dec 2007 Posts: 2
|
Posted: Thu Dec 27, 2007 10:18 pm Post subject: read file |
|
|
When I write and read file in the same activity everything is OK.
But when I try to read file from another activity I get "File not found" error.
I do use MODE_WORLD_READABLE option when I write the file, and I do see the file:
"/data/data/com.myname.android/files/JScript.htm"
I use
| Java: | FileInputStream fIn = openFileInput("/data/data/com.myname.android/files/JScript.htm") |
statement.
Help, please, to find mistake.
Thanks in advance. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
© 2007, Android Development Community
All rights reserved.
Powered by phpBB.
|