Working with Files

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

How to access a read-only package file on G1 phone?

Postby ppmoore » Thu Apr 02, 2009 5:52 pm

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

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: Select all
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: Select all
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
ppmoore
Freshman
Freshman
 
Posts: 9
Joined: Thu Apr 02, 2009 5:16 pm

Top

Postby songotho » Mon Apr 20, 2009 3:38 pm

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
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Postby ppmoore » Tue Apr 21, 2009 9:00 am

I'm sorry, I am an Android beginner. I can't help you.
ppmoore
Freshman
Freshman
 
Posts: 9
Joined: Thu Apr 02, 2009 5:16 pm

Postby shyjumon.n » Mon Apr 27, 2009 2:44 pm

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
shyjumon.n
Freshman
Freshman
 
Posts: 9
Joined: Wed Mar 18, 2009 8:13 am

any help?

Postby tripmills » Sat May 02, 2009 3:10 pm

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.
tripmills
Freshman
Freshman
 
Posts: 5
Joined: Sat May 02, 2009 3:00 pm

Re: any help?

Postby marcelloma » Thu May 07, 2009 9:53 pm

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?
marcelloma
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Mar 18, 2009 4:31 pm

Top

Postby tnagarajan » Wed Sep 30, 2009 5:33 pm

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.
tnagarajan
Freshman
Freshman
 
Posts: 2
Joined: Wed Sep 30, 2009 10:25 am

Postby afzkl » Fri Oct 02, 2009 3:35 pm

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.034 seconds, using GeSHi 1.0.8.4

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?
afzkl
Freshman
Freshman
 
Posts: 2
Joined: Thu Oct 01, 2009 4:10 pm

uses-permission for writing on sdcard

Postby sandeepz » Sat Nov 21, 2009 1:31 am

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>
sandeepz
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Nov 21, 2009 1:29 am

Writing userdefined objects to a file

Postby laksmi07 » Thu Nov 26, 2009 10:17 am

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
laksmi07
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 26, 2009 10:04 am

Packge PNG File

Postby rogerdodger » Mon Nov 30, 2009 8:45 am

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

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.                  in = openFileInput("wallpaper.png");
  3.                  mWallpaperManager.setStream(in);
  4.                  success = true;    
  5. }
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
rogerdodger
Junior Developer
Junior Developer
 
Posts: 21
Joined: Mon Nov 30, 2009 8:38 am

Re: Packge PNG File

Postby Johan Degraeve » Mon Nov 30, 2009 9:30 am

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
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Postby rahulsak » Tue Mar 02, 2010 10:48 am

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!! :)
rahulsak
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 10, 2010 7:16 am

Postby droider » Sun Apr 04, 2010 12:55 am

hi "+-",

Thanks for the tutorial. When I try to access the file - my emulator disappears and with that all the file explorer goes blank and I get this error on console:


[2010-04-03 19:54:00 - ddms]ADB rejected shell command (ls -l /data


What could be wrong ?
hmm...how about a Google Certified Android Developer certification?!
droider
Developer
Developer
 
Posts: 25
Joined: Sun Mar 21, 2010 6:17 am
Location: New York

I am trying to write a file to my sdcard (Not Emulator)

Postby manthena » Thu Apr 08, 2010 7:11 am

I am trying to write a file to my sdcard

code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.          File root = Environment.getExternalStorageDirectory();
  2.          File gpxfile = new File(root, "gpxfile.gpx");
  3.          FileWriter gpxwriter = new FileWriter(gpxfile);
  4.          BufferedWriter out = new BufferedWriter(gpxwriter);
  5.          out.write("Hello world");
  6.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Added the following in the AndroidManifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

I am getting the following Exception

java.io.FileNotFoundException: /sdcard/gpxfile.gpx
at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
at java.io.FileWriter.<init>(FileWriter.java:42)
manthena
Freshman
Freshman
 
Posts: 3
Joined: Thu Apr 08, 2010 6:53 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests