-1

Is it possible to create a file in SD card? or atleast copy a file from internal storage to SD card? I tried getExternalStorageDirectory() which returns the internal storage in some phones. Tried other ways to get tha path of the sd card. But file.mkdir() always returns false.

Sahana Prabhakar
  • 581
  • 1
  • 8
  • 21

2 Answers2

1

Don't forget about this permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

An example of creating files on SD Card is in another question.

Igor Staszewski
  • 401
  • 2
  • 8
0

The problem is that as you said getExternalStorageDirectory() does not return an SD card on every device. For Android it's called external because it's not private to your package, and can be accessed by other apps, and internal for Android is when only your app can access the files (e.g. it can be your database). There is no guarantee that you will get the SD card every time for every device.
Take a look at the comment to your question, it provides a useful link to the answer by CommonsWare, which explains the issue really well. And below you can find some workarounds like this one.

As for why mkdir() returns false, one reason may be the permission. I believe that the external storage is a part of dangerous permissions, therefore you need to acquire it during runtime and not only in the manifest.

Suleyman
  • 2,765
  • 2
  • 18
  • 31