Writing to external storage SD card

suggest change

You can also read and write from/to memory card (SD card) that is present in many Android devices. Files in this location can be accessed by other programs, also directly by the user after connecting device to PC via USB cable and enabling MTP protocol.

Finding the SD card location is somewhat more problematic. The Environment class contains static methods to get “external directories” that should normally be inside the SD card, also information if the SD card exists at all and is writable. This question contains valuable answers how to make sure the right location will be found.

Accessing external storage requires permissions in you Android manifest:

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

For older versions of Android putting permissions it is enough to put these permissions into manifest (the user must approve during installation). However starting from Android 6.0 Android asks the user for approval at the time of the first access, and you must support this new approach. Otherwise access is denied regardless of your manifest.

In Android 6.0, first you need to check for permission, then, if not granted, request it. The code examples can be found inside this SO question.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents