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 a removable SD card is somewhat more problematic. The Environment class has static methods for “external” directories. On many devices that primary external storage is emulated and is not a removable SD card. Those methods also report whether that storage exists and is writable. This question contains valuable answers on how to find the removable card.

Accessing external storage requires permissions in your Android manifest:

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

On older versions of Android, putting these permissions in the manifest is enough (the user approves them during installation). Starting with Android 6.0, Android asks the user at the time of the first access, and the app must support that request. Otherwise access is denied regardless of the 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:

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



Table Of Contents