2

My friend is testing my application which uses the SD card to store some settings. He has a Samsung Galaxy S2 but he just told me that he hasn't got an SD card in his device. It seems that the device created a folder in the phone's memory which simulates the existence of an SD card and that is where all app files that used the Sdcard are stored.

Is this a feature available for all Android devices? Should I consider the fact that there is no SD card on a device or should I not bother? Not sure if I should check for SD card availability in my app or not.

P.S. I've just noticed that the same goes for the emulator if I don't specify memory for the SD card.

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106

2 Answers2

1

You definitely should check for sdcard availability. On some devices it might work (as you said), but on some not, and you could get a FileNotFoundException.
So it's worth checking.

Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • 100% sure. This is because the external storage has a path, for example /mnt/sdcard/..... Now if you try to access that path, for example by trying to save something there, it will be unable to find the path because the external storage is missing. So the Android will tell you: "Sorry bro, but I don't find the path", in a more concise way by throwing a FileNotFoundException. – Andy Res Aug 08 '12 at 08:16
  • I failed to mention that i'm using SharedPreferences class to store my data. Does that change anything? – AndreiBogdan Aug 08 '12 at 08:19
  • If you store the data only in SharedPreferences, in private mode, then there shouldn't by any problems regardless if sdcard is available or not. – Andy Res Aug 08 '12 at 08:24
1
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.toString().equals(
                        state.toString())) {

  //////then do your work here////////

}

another solution: https://stackoverflow.com/a/7429264/6451573

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Furqi
  • 2,403
  • 1
  • 26
  • 32