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
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
ppmoore
Freshman
Freshman


Joined: 02 Apr 2009
Posts: 9

PostPosted: Thu Apr 02, 2009 5:52 pm    Post subject: How to access a read-only package file on G1 phone? Reply with quote

I've been reading the previous replies here, and they nearly answer my question, well, my two questions Smile

I've been experimenting with an application that reads a text file containing a dictionary of words. When I sign and publish this app, I want to include the text file in the package. At the moment, I keep the text file in the /data/app-private directory. On the emulator, I test this by first copying the text file to this directory using eclipse's DDMS perspective, and it works.

When I try to do the same on the G1 phone, I cannot copy the text file to the /data/app-private directory. Browsing the G1's file system using eclipse, I notice that there is a /data directory, but is empty. It seems that this is a read-only file system.

Q1: When preparing the package to install on the phone, how do I specify that the text file is also a part of the package? If the text file is part of the package, how do I specify its location? At the moment, my code specifies a hard-coded location like this:

Code:
String fileName = "file.txt";
String dirName = "/data/app-private";
File filePath = new File( dirName, fileName);
BufferedReader input =  new BufferedReader(new FileReader(aFileName));
try
{
  // Loop to count lines in the file
  while( input.readLine() != null )
  {
    i++;
  }
}


Q2: I suppose I should avoid hard-coding the file's location. I read earlier in this thread that I should use the activity method openFileInput(). Do I need to specify the file's location in this case?

Code:
FileInputStream stream = openFileInput("file.txt");
InputStreamReader osw = new InputStreamReader(stream);


Thanks again for the great site. I read earlier that you were studying for exams. How do you find the time?

Many thanks,
Paul

Can someone help?
Paul
Back to top
View user's profile Send private message
songotho
Developer
Developer


Joined: 03 Mar 2009
Posts: 35

PostPosted: Mon Apr 20, 2009 3:38 pm    Post subject: Reply with quote

Hi,

The tutorial is work with file, so I would not find any introductions about file zip. Would you introduce about working with file zip (i.e: zip and unzip file...). How do I secure the file in Android?

Thanks,

--Alex
Back to top
View user's profile Send private message
ppmoore
Freshman
Freshman


Joined: 02 Apr 2009
Posts: 9

PostPosted: Tue Apr 21, 2009 9:00 am    Post subject: Reply with quote

I'm sorry, I am an Android beginner. I can't help you.
Back to top
View user's profile Send private message
shyjumon.n
Freshman
Freshman


Joined: 18 Mar 2009
Posts: 9

PostPosted: Mon Apr 27, 2009 2:44 pm    Post subject: Reply with quote

Hi plusminus,
Any idea how can i save a portion of image into a file. Eg: 480x320 image i want to cut into say 4 pieces and save to different files, based on a coordinate in the picture.

Thank you for any help.
SMN
Back to top
View user's profile Send private message
tripmills
Freshman
Freshman


Joined: 02 May 2009
Posts: 5

PostPosted: Sat May 02, 2009 3:10 pm    Post subject: any help? Reply with quote

Thanks for this great stuff. I am not new to programming (about 30yrs), but i have always stayed away from java up until now.

i would kill for some help to my little problem and i bet it is a simple solution that i just cant see/grasp right now.

here is my issue:
i wrote a game and i want it to save the highscore. i have a thread within a view extended from the main activity. The closest i got to success was to create another class extended from the main activity but everytime i reference it from the thread/view i force close.

any thoughts would be helpful - thanks.
Back to top
View user's profile Send private message
marcelloma
Junior Developer
Junior Developer


Joined: 18 Mar 2009
Posts: 12

PostPosted: Thu May 07, 2009 9:53 pm    Post subject: Re: any help? Reply with quote

tripmills wrote:
Thanks for this great stuff. I am not new to programming (about 30yrs), but i have always stayed away from java up until now.

i would kill for some help to my little problem and i bet it is a simple solution that i just cant see/grasp right now.

here is my issue:
i wrote a game and i want it to save the highscore. i have a thread within a view extended from the main activity. The closest i got to success was to create another class extended from the main activity but everytime i reference it from the thread/view i force close.

any thoughts would be helpful - thanks.


Have you tried saving it on a file? Or using the Bundle object?
Back to top
View user's profile Send private message
tnagarajan
Freshman
Freshman


Joined: 30 Sep 2009
Posts: 2

PostPosted: Wed Sep 30, 2009 5:33 pm    Post subject: Reply with quote

Hello Experts,

Can anybody please tell me, how to write and read a text file outside the application path.
And also how can i include this, while packaging.

Regards,
Nagarajan T.
Back to top
View user's profile Send private message
afzkl
Freshman
Freshman


Joined: 01 Oct 2009
Posts: 2

PostPosted: Fri Oct 02, 2009 3:35 pm    Post subject: Reply with quote

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();
          }

char[] inputBuffer = new char[TESTSTRING.length()];

Thanks works great but I have one small issue. How should I know how big the inputBuffer char array should be if i don't know the length of the textfile?
Back to top
View user's profile Send private message
sandeepz
Once Poster
Once Poster


Joined: 21 Nov 2009
Posts: 1

PostPosted: Sat Nov 21, 2009 1:31 am    Post subject: uses-permission for writing on sdcard Reply with quote

Also note that, need to add permission in AndroidManifest.xml for writing files on sdcard:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Back to top
View user's profile Send private message
laksmi07
Freshman
Freshman


Joined: 26 Nov 2009
Posts: 2

PostPosted: Thu Nov 26, 2009 10:17 am    Post subject: Writing userdefined objects to a file Reply with quote

Hi I am a novice in android.
I want to write user defined objects to a file. Is it possible to do it with files or it has to be done using input output streams. RIM has persisantstorage for saving objects while for j2me its a round about work still we have rms. What is the option other than sqlite?

Thanks
Lakshmi
Back to top
View user's profile Send private message
rogerdodger
Junior Developer
Junior Developer


Joined: 30 Nov 2009
Posts: 15

PostPosted: Mon Nov 30, 2009 8:45 am    Post subject: Packge PNG File Reply with quote

I am new to android development....

Can someone please explain how to package a PNG (for example wallpaper.png) in my package? I've been searching everywhere and can't figure this out.

When I add it to my package in Eclipse /src/com.my_package/files/wallpaper.png it doesn't exist on the device emulator.

I'm trying to set it to the system wallpaper, but i keep getting FileNotFound exception /data/data/com.my_package/files/wallpaper.png

So how can i package a PNG file in my package so that i can open it?

Here is how i am trying to open it

Java:

try {
           in = openFileInput("wallpaper.png");
              mWallpaperManager.setStream(in);
              success = true;    
}
Back to top
View user's profile Send private message
Johan Degraeve
Developer
Developer


Joined: 27 Oct 2009
Posts: 40
Location: Belgium

PostPosted: Mon Nov 30, 2009 9:30 am    Post subject: Re: Packge PNG File Reply with quote

rogerdodger wrote:
I am new to android development....

Can someone please explain how to package a PNG (for example wallpaper.png) in my package? I've been searching everywhere and can't figure this out.



you need to put your file in your res/drawable folder , see
resources
The id to use in this method, this you can find in your R class, which is automatically generated by Eclipse, as soon as you add your source file to your res folder

_________________
regards,

Johan
Back to top
View user's profile Send private message Visit poster's website
rahulsak
Freshman
Freshman


Joined: 10 Feb 2010
Posts: 2

PostPosted: Tue Mar 02, 2010 10:48 am    Post subject: Reply with quote

hello,
I am a beginner in android.
I want to know hw to create xml file from program.
Actually i want to collect data from GUI and store that data to new xml file.
plz give me some solution to this.

Thanks in Advance!! Smile
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
Page 5 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.