Write a text file to SD card

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

Write a text file to SD card

Postby kognos » Thu Sep 01, 2011 10:00 am

I am using the following code to write a text file to the SD card:

File sdRoot = null;
File dataFile = null;
FileWriter fWriter;
...
try {
sdRoot = Environment.getExternalStorageDirectory();
dataFile = new File(sdRoot, "myFile.txt");
fWriter = new FileWriter(dataFile, true);
fWriter.write("data as string");
fWriter.flush();
fWriter.close();
}
catch ...

This works fine in the emulator, and writes the file to the top level of the SD card.

However on a device, this puts the file into the main device storage,not the SDD card. Any clues about why this happens? I have tried two tablets, a Huawei Ideos and an MID X5A. The SD card in general works fine in both devices - I can download files onto the card and access them with File Explorer. I am looking for generic code, rather then specifying a literal path, as I want the code to work on any device with external storage.

I'm also having problems with the file on the Huawei. The file is written to the top level of the device storage, I can read it from the application and also with File Explorer, but when I connect up via USB, my computer can't see this file. It sees everything else. I need to be able to upload the data file. Help appreciated.
kognos
Freshman
Freshman
 
Posts: 2
Joined: Thu Sep 01, 2011 9:34 am

Top

Re: Write a text file to SD card

Postby jonbonazza » Thu Sep 01, 2011 4:52 pm

I'm really not sure why this happens, but as a test, you could try chaning
Code: Select all
sdcard = Environment.getExternalStorageDirectory();


to a manual definition, like so:
Code: Select all
sdcard = new File("/sdcard/");


Doing it that way forces the file object to point to the sdcard directory. There might be an error in the Environment.getExternalStorageDirectory(). Hard to say.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Write a text file to SD card

Postby Aseriesofsmallprojects » Wed Sep 07, 2011 12:20 am

Are you only testing with tablets? This might be a configuration issue - as in, baked-in value from the environment isn't what you want it to be.

I know that some devices are configured differently than my phones. For example, the Xoom now enabled the SD card, but the "external storage" actually gets you the partition on the internal memory and you can't write to the SD card from the device at all.

It might be necessary to insert a check that will "correct" the path if certain indicators show that you have the wrong one, but that certainly wouldn't be ideal.
www.ProjectJourneyman.com - Android development and app marketing
twitter.com/PrjJourneyman Twitter feed
Android Developer Tutorial A growing tutorial for Android Devs
The Android Income Series Books on income topics for Android developers
Aseriesofsmallprojects
Developer
Developer
 
Posts: 39
Joined: Fri Oct 08, 2010 4:29 pm
Location: USA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 10 guests