0

I'm trying to create a file in the external storage (SD Card) of my phone.

File F = new File(Environment.getExternalStorageDirectory() + File.separator + "MyBrand" + File.separator + "MyApp" + File.separator + "MyFile.srl");

but this is creating it in the Internal Storage area. I checked in Windows Explorer. When debugging, the path of F is "/mtn/sdcard/MyBrand/MyApp/MyFile.srl", which is wrong because in Windows Explorer the name of the sdcard is really "SD Card" (with a space). I also tried .getAbsolutePath at the end of getExternalStorageDirectory()` but it did the exact same thing.

My code for creating this file:

if (F.exists() == false)
{
  try
  {
    F.getParentFile().getParentFile().mkdir();
    F.getParentFile().mkdir();
    F.createNewFile();
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }

  scr_save();
}
else
  scr_load();

It does the mkdirs() and createNewFile() just fine, but scr_save and scr_load always throw exceptions, either FileNotFound or IOException.

EDIT: I've also been testing it when not plugged into my computer, just to make sure of no interference. To be safe, i always delete the paths and file before unplugging it. It seems to create the dirs and file, but then not reading it right (could it be not serialized properly? i use this file to store serialized objects).

EDIT: there was an error originally. I was putting in the path + filename as a single parameter to F. Now I pass the path and filename as 2 separate strings. However, i'm still experiencing the same problem :(

I have the correct permission in my manifest (.WRITE_EXTERNAL_STORAGE)

I use Windows 7 64-bit, and also the 64-bit version of Eclipse. My phone is a LG-MS770.

Anyway please help. No clue why the getExternalSorage is not working.

DrZ214
  • 486
  • 5
  • 19

3 Answers3

1

I'm trying to create a file in the external storage (SD Card) of my phone

What the Android SDK refers to as "external storage" is not an SD card, on most devices manufactured since early 2011.

No clue why the getExternalSorage is not working.

It is working just fine and as documented. It is simply not meeting your expectations.

Up until Android 4.4, there was no documented and supported means of accessing any sort of removable storage. On Android 4.4, you now have getExternalFilesDirs() and getExternalCacheDirs() (note the plural), which may give you access to specific directories on removable storage if such storage exists.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • ok thx for clearing that up. so i dont want the actual SD card, just the "External Storage" as defined by that android operation. – DrZ214 Feb 16 '14 at 23:01
0

First of all, /mnt/sdcard/MyBrand/MyApp/MyFile.srl is correct: SD Card can be a label assigned by Windows Explorer.
Anyway, take a look at getExternalStorageDirectory documentation where it says:

This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

Likely, you debug when the SD Card is mounted and this is wrong.
Then, mkdir does not throw exception if it fails; createNewFile should do it, but when you read something like "or some other problem has happened", you should prepare yourself for headaches.

Kheldar
  • 5,361
  • 3
  • 34
  • 63
  • yes, ive been testing both plugged in and unplugged from the computer. i added an edit to my OP. – DrZ214 Feb 16 '14 at 23:00
0

The getExternalStorage is pretty confusing. It does not represent the SD card you plug in your phone, but the content that will be displayed on your computer when you plug your phone in.

This is the Storage you can browse to put some images, music, etc. on your phone or get them from your phone.

TheCopycat
  • 401
  • 2
  • 6