Reading and writing files on an sdcard

Put your problem here if it does not fit any of the other categories.

Reading and writing files on an sdcard

Postby jasonpeinko » Sun Nov 16, 2008 8:28 am

Im trying to read a file from the sdcard, later i will eventually want to write back to a file, or add more.

But reading this file does not work, it has been created and is on the sdcard in the emulator:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  try { // catches IOException below
  3.  
  4.                     final String TESTSTRING = new String("Hello Android");
  5.  
  6.                    
  7.  
  8.  
  9.  
  10.                     FileInputStream fIn = openFileInput("/sdcard/Notes/mynote1.txt");
  11.  
  12.                     InputStreamReader isr = new InputStreamReader(fIn);
  13.  
  14.                     /* Prepare a char-Array that will
  15.  
  16.                      * hold the chars we read back in. */
  17.  
  18.                     char[] inputBuffer = new char[TESTSTRING.length()];
  19.  
  20.                     // Fill the Buffer with data from the file
  21.  
  22.                     isr.read(inputBuffer);
  23.  
  24.                     // Transform the chars to a String
  25.  
  26.                     String readString = new String(inputBuffer);
  27.  
  28.                    
  29.  
  30.                     // Check if we read back the same chars that we had written out
  31.  
  32.                     boolean isTheSame = TESTSTRING.equals(readString);
  33.  
  34.                     //Notes.add(createItem(currentFile.getAbsolutePath().substring(cut+1),readString));
  35.  
  36.                     // WOHOO lets Celebrate =)
  37.  
  38.                     Log.i("File Reading stuff", "success = " + isTheSame);
  39.  
  40.  
  41.  
  42.                } catch (IOException ioe) {
  43.  
  44.                     ioe.printStackTrace();
  45.  
  46.                }
  47.  
  48.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


I just get the force close window.
jasonpeinko
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Nov 05, 2008 7:30 am

Top

Re: Reading and writing files on an sdcard

Postby MrSnowflake » Sun Nov 16, 2008 11:16 am

jasonpeinko wrote:I just get the force close window.
Check you LOG!!!

Did you load the sd image in the emulator (using option: -sdcard sd.img). And keep in mind the users of your app might not have a SD card in their phone, it's not good if their app craches instead of giving a nice AlertDialog.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby jasonpeinko » Sun Nov 16, 2008 7:21 pm

Ive been trying to figure out how to view a log but I cant!

Also, if i just save files to the program directory will they stay on the device?
jasonpeinko
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Nov 05, 2008 7:30 am

Postby MrSnowflake » Sun Nov 16, 2008 9:05 pm

jasonpeinko wrote:Ive been trying to figure out how to view a log but I cant!
on a commandline: "adb logcat"

jasonpeinko wrote:Also, if i just save files to the program directory will they stay on the device?
Yes, of course.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby jasonpeinko » Sun Nov 16, 2008 9:14 pm

Im getting this error:

File /sdcard/Notes/mynote1.txt contains a path separator

would it be better to just store files in the app dir, or both?
jasonpeinko
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Nov 05, 2008 7:30 am

Postby MrSnowflake » Sun Nov 16, 2008 9:37 pm

This is what the docs say:
openFileInput(String name)
Open a private file associated with this Context's application package for reading.
Parameters
name The name of the file to open; can not contain path separators.

openFileInput is only for local files I think. Try using File("path/bla").
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 18 guests