Home > Enterprise >  How can I write (make directory, write file, ...) on a real sd card
How can I write (make directory, write file, ...) on a real sd card

Time:11-12

I want to create a folder and write a file in real external storage (real extern sd card). I can write to the internal storage, the external storage, but not to the external sd card (I mean the external storage card you put into a cell phone for more space to store images, videos, ...). The path of the external sd-card is: "/storage/1234-5678/" and it is on a samsung smartphone. Reading from the external sd card works without problems. I am testing with Android 8 (and later with higher versions).

I have searched through internet and try but not getting the result, I have added permissions in Android Manifest file as well.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="29"
    tools:ignore="ScopedStorage" />

and an easy test is:

File directory = new File("/storage/1234-5678/new_folder/");
if (!directory.exists()) {
    boolean ok = directory.mkdirs();
}

The result to ok is always false, when I use the path to the external / removable sd card.
What I am doing wrong?

CodePudding user response:

Since providing a direct path might be a problem. So I would recommend using the Android file access mechanism.

Mentioned here->https://developer.android.com/training/data-storage/app-specific#external-select-location

CodePudding user response:

Have a look at the second item returned by getExternalFilesDirs().

You can write on that location of the removable micro sd card.

  • Related