| Author |
Message |
krystox Freshman

Joined: 22 Jan 2008 Posts: 7
|
Posted: Tue Jan 22, 2008 9:47 am Post subject: |
|
|
Hi Cabernet1976,
One further issue is even I can create a dir somewhere, there is no way I can read or write a file there.
|
|
| Back to top |
|
 |
|
|
 |
snowtiger Developer

Joined: 21 Dec 2007 Posts: 34
|
Posted: Tue Jan 22, 2008 10:31 am Post subject: |
|
|
I also had a lot of problems with creating files and directories.
Once the Notepad example was working and created its database. In my app I tried everything and I couldn't even create a directory. So I tried again with Notepad, deleted the database there and tried to start Notepad. Same error (Nullpointer) as in my app. Android didn't want to create the database anymore (with the same code that created the database once before).
So, what to do? I just went to command line and did a: and everything works fine again the next time I started the emulator...  |
|
| Back to top |
|
 |
eugenk Developer

Joined: 30 Jan 2008 Posts: 29 Location: Russian Federation, Moscow
|
Posted: Sun Feb 10, 2008 8:23 am Post subject: |
|
|
One strange and unpleasant thing. Sometimes | Java: | FileOutputStream fos = openFileOutput(name, MODE); | does not work and causes FileNotFoundException. It depends on application. Code from plusminus works itself very well. But when I copy this code directly to onCreate() method in my application, it stops works and FileNotFoundException appears. Small examination with debugger shows, that exception has detailMessage member with diagnostics for example "/files/samplefile.txt" I.e. for some reason, application sets data path as /files/ instead /data/data/your_project_package_structure/files/ . It is very likely, that it is SDK bug. Now I work to find out conditions, when it appears. If we take plusminus code, and change | Java: | FileInputStream fIn = openFileInput("samplefile.txt"); | to | Java: | FileInputStream fIn = openFileInput("samp__lefile.txt"); | for example, FileNotFoundException appears, because we write file samplefile.txt, but try to read from samp__lefile.txt.
We can see it's detailMessage: "/data/data/org.anddev.android.workingwithfiles/files/samp__lefile.txt". I.e. in this case data path is correct: /data/data/org.anddev.android.workingwithfiles/files/ Now I try to destroy plusminus example to obtain this bug in pure, and will write about my results later.
P.S. About prefixes. Many peoples ask it. It do not works !!!. Guys, please, READ DOCUMENTATION !!!. You'll save and your and others time See Context.openFileOutput() doc for example:
public abstract FileOutputStream openFileOutput(String name, int mode)
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
Parameters
name The name of the file to open; can not contain path separators.
mode Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_APPEND to append to an existing file, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.
Returns
* FileOutputStream Resulting output stream.
Throws
FileNotFoundException java.io.FileNotFoundException
Another cool thing is a debugger. If you add in plusminus example path prefix, i.e. write | Java: | FileOutputStream fOut = openFileOutput("/data/data/org/anddev.android.workingwithfiles/files/samplefile.txt", MODE_WORLD_READABLE); | FileNotFoundException appears. See with debugger it's detailMessage member. You'll see text:
"File /data/data/org/anddev.android.workingwithfiles/files/samplefile.txt contains a path separator"
And can understand, that path prefix is not allowed in filename. |
|
| Back to top |
|
 |
rohan Developer

Joined: 19 Mar 2008 Posts: 34
|
|
| Back to top |
|
 |
Jesmiatka Senior Developer

Joined: 04 Feb 2008 Posts: 164 Location: Netherlands
|
Posted: Wed Mar 19, 2008 3:41 pm Post subject: |
|
|
Start an emulator trough eclipse. You can do this by running an application as Android Application.
This will show the open emulator device in that box. |
|
| Back to top |
|
 |
rohan Developer

Joined: 19 Mar 2008 Posts: 34
|
Posted: Wed Mar 19, 2008 3:47 pm Post subject: |
|
|
| Jesmiatka wrote: | Start an emulator trough eclipse. You can do this by running an application as Android Application.
This will show the open emulator device in that box. |
Thanx Jesmiatka,
I am already running my android application , but still am not gettin anything in devices. It is not showing any open emulator device in that box.  |
|
| Back to top |
|
 |
|
|
 |
rohan Developer

Joined: 19 Mar 2008 Posts: 34
|
Posted: Wed Mar 19, 2008 4:00 pm Post subject: |
|
|
| Jesmiatka wrote: | Start an emulator trough eclipse. You can do this by running an application as Android Application.
This will show the open emulator device in that box. |
thanx Jesmiatka,
I got the emulator in devices. by by running an application as Android Application. |
|
| Back to top |
|
 |
rohan Developer

Joined: 19 Mar 2008 Posts: 34
|
Posted: Thu Mar 20, 2008 8:12 pm Post subject: Re: Working with Files |
|
|
| plusminus wrote: | 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 |
hi plusminus,
I am not gettin "Log.i("File Reading stuff", "success = " + isTheSame);". wht is Log and where is this Log stored , i mean how to see the log output.
thanx. |
|
| Back to top |
|
 |
Mick Freshman

Joined: 24 Mar 2008 Posts: 6 Location: France
|
Posted: Mon Mar 24, 2008 10:03 am Post subject: |
|
|
Hello
I want to know if it is possible to create a file in the /res directory, because I have to create an animation xml file, and I have to put it in the /res/anim directory.
Thanks a lot 
Mick |
|
| Back to top |
|
 |
loki Once Poster

Joined: 11 Apr 2008 Posts: 1
|
Posted: Fri Apr 11, 2008 4:23 pm Post subject: |
|
|
hi folks.
first post in this forum, hope you guys can help me.
my problem is the following: currently i am developing an application which is divided into background service and foreground GUI. the service is currently realized as a thread, maybe will evolve into a real service in the future.
the problem is now: i want to write to a file from the service, but the service does not know anything about the ApplicationContext, only the GUI does.
i will now implement a workaround (giving the service an ApplicationContext field and assigning the first Activity to it), but that is not a very clean solution.
any other idea how to solve this issue?
thanks in advance,
regards,
loki |
|
| Back to top |
|
 |
d4d4r Freshman

Joined: 12 Apr 2008 Posts: 8
|
Posted: Fri Jul 25, 2008 3:06 am Post subject: How About Midi File? |
|
|
how can i read and write midi file in android?
if i not mistaken, android has midifile reader and midifile writer..
But how to use it?
Thx before.. |
|
| Back to top |
|
 |
AJ Freshman

Joined: 08 Jul 2008 Posts: 2
|
Posted: Mon Jul 28, 2008 9:57 pm Post subject: Working with files |
|
|
Hey guys,
I tried plusminus's code on "working with files" but i'm not getting any output neither on the screen or my package folder.
Can you guys please help me?
Thanks |
|
| Back to top |
|
 |
deepakpengoria Junior Developer

Joined: 08 Aug 2008 Posts: 11
|
Posted: Fri Aug 08, 2008 10:54 am Post subject: |
|
|
hello plusminus,
thanks a lot for your posts.
I have small doubt, how to write data into file in the form of bytes.
Thank u in advance,
regards
Deepak |
|
| Back to top |
|
 |
deepakpengoria Junior Developer

Joined: 08 Aug 2008 Posts: 11
|
Posted: Tue Aug 12, 2008 8:23 pm Post subject: |
|
|
Thanks a lot for the tutorial,
I would like to ask a question.
I want the file to save the formatted text in file and then retrieve it back. can someone help me in this regard.
Regards
deepakpengoria |
|
| Back to top |
|
 |
beeshop Freshman

Joined: 12 Aug 2008 Posts: 5
|
Posted: Fri Aug 29, 2008 5:25 pm Post subject: |
|
|
Hi all.
i've got a problem with this topic.
I cannot view the files directory in "/data/data/your_project_package_structure/files/"
only "/data/data/your_project_package_structure/" . Must I create it ?
How can I read a file straigth to the output, placing the file in /data/data/your_project_package_structure/files/ ? ( when I see it) .
thanks! |
|
| Back to top |
|
 |
|