andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Working with Files

Goto page Previous  1, 2, 3, 4, 5  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
krystox
Freshman
Freshman


Joined: 22 Jan 2008
Posts: 7

PostPosted: Tue Jan 22, 2008 9:47 am    Post subject: Reply with quote

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.

Sad

cabernet1976 wrote:
Hi krystox,

I met the same issus ( http://www.anddev.org/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-developers/browse_thread/thread/64fb55b5bd03a2cd , it is said that somebody has done it successfully.
Hope you can make it work.
Back to top
View user's profile Send private message
snowtiger
Developer
Developer


Joined: 21 Dec 2007
Posts: 34

PostPosted: Tue Jan 22, 2008 10:31 am    Post subject: Reply with quote

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:
cmd:
emulator -wipe-data
and everything works fine again the next time I started the emulator... Smile
Back to top
View user's profile Send private message
eugenk
Developer
Developer


Joined: 30 Jan 2008
Posts: 29
Location: Russian Federation, Moscow

PostPosted: Sun Feb 10, 2008 8:23 am    Post subject: Reply with quote

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 Smile 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
View user's profile Send private message Send e-mail
rohan
Developer
Developer


Joined: 19 Mar 2008
Posts: 34

PostPosted: Wed Mar 19, 2008 3:20 pm    Post subject: Not gettin the devices tab Reply with quote

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


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.
Back to top
View user's profile Send private message
Jesmiatka
Senior Developer
Senior Developer


Joined: 04 Feb 2008
Posts: 164
Location: Netherlands

PostPosted: Wed Mar 19, 2008 3:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
rohan
Developer
Developer


Joined: 19 Mar 2008
Posts: 34

PostPosted: Wed Mar 19, 2008 3:47 pm    Post subject: Reply with quote

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. Sad
Back to top
View user's profile Send private message
rohan
Developer
Developer


Joined: 19 Mar 2008
Posts: 34

PostPosted: Wed Mar 19, 2008 4:00 pm    Post subject: Reply with quote

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. Very Happy by by running an application as Android Application.
Back to top
View user's profile Send private message
rohan
Developer
Developer


Joined: 19 Mar 2008
Posts: 34

PostPosted: Thu Mar 20, 2008 8:12 pm    Post subject: Re: Working with Files Reply with quote

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.

Question Problems/Questions: Write them right below...

Difficulty: 1 of 5 Smile

What it will look like:
Laughing This is the first tutorial without a screenshot Laughing


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 Twisted Evil 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 Exclamation
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.


Idea 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
View user's profile Send private message
Mick
Freshman
Freshman


Joined: 24 Mar 2008
Posts: 6
Location: France

PostPosted: Mon Mar 24, 2008 10:03 am    Post subject: Reply with quote

Hello Wink

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 SmileSmile
Mick
Back to top
View user's profile Send private message
loki
Once Poster
Once Poster


Joined: 11 Apr 2008
Posts: 1

PostPosted: Fri Apr 11, 2008 4:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
d4d4r
Freshman
Freshman


Joined: 12 Apr 2008
Posts: 8

PostPosted: Fri Jul 25, 2008 3:06 am    Post subject: How About Midi File? Reply with quote

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
View user's profile Send private message
AJ
Freshman
Freshman


Joined: 08 Jul 2008
Posts: 2

PostPosted: Mon Jul 28, 2008 9:57 pm    Post subject: Working with files Reply with quote

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
View user's profile Send private message
deepakpengoria
Junior Developer
Junior Developer


Joined: 08 Aug 2008
Posts: 11

PostPosted: Fri Aug 08, 2008 10:54 am    Post subject: Reply with quote

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
View user's profile Send private message
deepakpengoria
Junior Developer
Junior Developer


Joined: 08 Aug 2008
Posts: 11

PostPosted: Tue Aug 12, 2008 8:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
beeshop
Freshman
Freshman


Joined: 12 Aug 2008
Posts: 5

PostPosted: Fri Aug 29, 2008 5:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page Previous  1, 2, 3, 4, 5  Next
Page 3 of 5

 
Jump to:  
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.