This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Android and the fly likes read/write file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Mobile » Android
Bookmark "read/write file " Watch "read/write file " New topic
Author

read/write file

Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

Good morning,
Is there any way to read/write a file from a class without extending the Activity? I am tryint to read a file from a Fragment and i cant make it work...

thanks in advance.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5850
    
    7

You should be able to read/write a file from any class. In one of my apps I and reading/writing a file from an ArrayAdaptor.

Perhaps you need to show some code, them we might be able to help you better.


JBoss In Action
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

Hello again,
Below is my class code,


how can i create a class and use this piece of code. If i put the code in the fragment class, MODE_WORLD_READABLE cannot be resolved to a variable error is displayed. Only when i add this code to an activity it works.
In general i get a byte[] array and write it to a file. When this was on the activity class it worked great. When i added it to a fragment class it displays the error above.

thanks in advance for the help.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5850
    
    7

MODE_WORLD_READABLE is defined in Context, and Activity is a subclass of Context. But since you move the code out of the Activity, you need to tell the compiler (and Eclipse) where MODE_WORLD_READABLE comes from, so use Context.MODE_WORLD_READABLE instead.

http://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE
 
Don't get me started about those stupid light bulbs.
 
subject: read/write file