Working with Files

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby krystox » Tue Jan 22, 2008 9:47 am

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.

:(

cabernet1976 wrote:Hi krystox,

I met the same issus ( create_directory_failed_in_file_system-t573.html ), I did it according to a thread in google group but failed.
The thread is http://groups.google.com/group/android- ... b5bd03a2cd , it is said that somebody has done it successfully.
Hope you can make it work.
krystox
Freshman
Freshman
 
Posts: 7
Joined: Tue Jan 22, 2008 2:39 am

Top

Postby snowtiger » Tue Jan 22, 2008 10:31 am

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:
Syntax: [ Download ] [ Hide ]
  1. emulator -wipe-data 
and everything works fine again the next time I started the emulator... :-)
snowtiger
Developer
Developer
 
Posts: 34
Joined: Fri Dec 21, 2007 10:44 am

Postby eugenk » Sun Feb 10, 2008 8:23 am

One strange and unpleasant thing. Sometimes
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileOutputStream fos = openFileOutput(name, MODE);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
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
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileInputStream fIn = openFileInput("samplefile.txt");
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
to
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileInputStream fIn = openFileInput("samp__lefile.txt");
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
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
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileOutputStream fOut = openFileOutput("/data/data/org/anddev.android.workingwithfiles/files/samplefile.txt", MODE_WORLD_READABLE);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
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.
eugenk
Developer
Developer
 
Posts: 29
Joined: Wed Jan 30, 2008 8:07 pm
Location: Russian Federation, Moscow

Not gettin the devices tab

Postby rohan » Wed Mar 19, 2008 3:20 pm

plusminus wrote:Hello rtehman,

This way to get to the DDMS-Perspective in Eclipse (if A isn't visible, do B):
Image

On the Bottom you will see the so called "LOGCAT"-View appear where the Logs are written to.

Here switch to the File Explorer.
Image


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


hi plusminus,
I am not gettin the devices tab in my eclipse. I mean there is nothing in the devices tab (its empty). So i cannot transfer files .What should i do? please help.(new to android)
Thanx.
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Postby Jesmiatka » Wed Mar 19, 2008 3:41 pm

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.
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby rohan » Wed Mar 19, 2008 3:47 pm

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. :(
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Top

Postby rohan » Wed Mar 19, 2008 4:00 pm

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. :D by by running an application as Android Application.
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Re: Working with Files

Postby rohan » Thu Mar 20, 2008 8:12 pm

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:
:lol: This is the first tutorial without a screenshot :lol:


Description:
To understand why we cannot use standard java file access, like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileWriter f = new FileWriter("impossible.txt");
  2. // throws: 'java.io.FileNotFoundException: /impossible.txt '
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

, 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 :twisted: 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 :!:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileWriter f = new FileWriter("/sdcard/download/possible.txt");
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

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...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         try { // catches IOException below
  2.                         final String TESTSTRING = new String("Hello Android");
  3.                        
  4.                         // ##### Write a file to the disk #####
  5.                         /* We have to use the openFileOutput()-method
  6.                          * the ActivityContext provides, to
  7.                          * protect your file from others and
  8.                          * This is done for security-reasons.
  9.                          * We chose MODE_WORLD_READABLE, because
  10.                          *  we have nothing to hide in our file */             
  11.                         FileOutputStream fOut = openFileOutput("samplefile.txt",
  12.                                                                 MODE_WORLD_READABLE);
  13.                         OutputStreamWriter osw = new OutputStreamWriter(fOut); 
  14.  
  15.                         // Write the string to the file
  16.                         osw.write(TESTSTRING);
  17.                         /* ensure that everything is
  18.                          * really written out and close */
  19.                         osw.flush();
  20.                         osw.close();
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

2.Reading the file back...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         // ##### Read the file back in #####
  2.                        
  3.                         /* We have to use the openFileInput()-method
  4.                          * the ActivityContext provides.
  5.                          * Again for security reasons with
  6.                          * openFileInput(...) */
  7.                         FileInputStream fIn = openFileInput("samplefile.txt");
  8.                         InputStreamReader isr = new InputStreamReader(fIn);
  9.                         /* Prepare a char-Array that will
  10.                          * hold the chars we read back in. */
  11.                         char[] inputBuffer = new char[TESTSTRING.length()];
  12.                         // Fill the Buffer with data from the file
  13.                         isr.read(inputBuffer);
  14.                         // Transform the chars to a String
  15.                         String readString = new String(inputBuffer);
  16.                        
  17.                         // Check if we read back the same chars that we had written out
  18.                         boolean isTheSame = TESTSTRING.equals(readString);
  19.  
  20.                         // WOHOO lets Celebrate =)
  21.                         Log.i("File Reading stuff", "success = " + isTheSame);
  22.  
  23.                 } catch (IOException ioe) {
  24.                         ioe.printStackTrace();
  25.                 }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


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.


:idea: You delete a file with the following method coming from ApplicationContext:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public boolean deleteFile(String name)
  2. // Delete the given private file associated with this Context's application package.
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


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.
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Postby Mick » Mon Mar 24, 2008 10:03 am

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
Mick
Freshman
Freshman
 
Posts: 6
Joined: Mon Mar 24, 2008 9:59 am
Location: France

Postby loki » Fri Apr 11, 2008 4:23 pm

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
loki
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 11, 2008 4:16 pm

How About Midi File?

Postby d4d4r » Fri Jul 25, 2008 3:06 am

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..
d4d4r
Freshman
Freshman
 
Posts: 8
Joined: Sat Apr 12, 2008 9:06 am

Working with files

Postby AJ » Mon Jul 28, 2008 9:57 pm

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
AJ
Freshman
Freshman
 
Posts: 2
Joined: Tue Jul 08, 2008 1:18 am

Postby deepakpengoria » Fri Aug 08, 2008 10:54 am

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
deepakpengoria
Junior Developer
Junior Developer
 
Posts: 11
Joined: Fri Aug 08, 2008 9:12 am

Postby deepakpengoria » Tue Aug 12, 2008 8:23 pm

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
deepakpengoria
Junior Developer
Junior Developer
 
Posts: 11
Joined: Fri Aug 08, 2008 9:12 am

Postby beeshop » Fri Aug 29, 2008 5:25 pm

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!
beeshop
Freshman
Freshman
 
Posts: 5
Joined: Tue Aug 12, 2008 9:13 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests