0

I am building a media player application and I want to make a list from all .mp3 files on the device. To do that I need the EXTERNAL REMOVABLE storage path.

This path could be different in different devices.

Environment.getExternalStorageDirectory().getAbsolutePath()

I've already tried above line of code but it returns the path of internal storage.

This has been discussed here! It didn't help.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Have a look at getExternalFilesDirs(). If a microSD cart is inserted it will retuen an array with two elements. The second item points to the app specific directory on the micro SD card. From there you can get down to the root of the card. – blackapps Nov 01 '19 at 14:54
  • But for Android Q using a classic file path makes no sense. You better use ACTION_OPEN_DOCUMENT_TREE to let the user choose the micro SD card. And after that use the Storage Access Framework to list the mp3's. Your media player should be able to read from content schemes otherwise it is soon not usable. – blackapps Nov 01 '19 at 14:57

1 Answers1

0

You can achieve it through

public static String getExternalSdCardPath() {
    String path = null;

    File sdCardFile = null;
    List<String> sdCardPossiblePath = Arrays.asList("external_sd", "ext_sd", "external", "extSdCard");

    for (String sdPath : sdCardPossiblePath) {
            File file = new File("/mnt/", sdPath);

            if (file.isDirectory() && file.canWrite()) {
                    path = file.getAbsolutePath();

                    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
                    File testWritable = new File(path, "test_" + timeStamp);

                    if (testWritable.mkdirs()) {
                            testWritable.delete();
                    }
                    else {
                            path = null;
                    }
            }
    }

    if (path != null) {
            sdCardFile = new File(path);
    }
    else {
            sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    }

    return sdCardFile.getAbsolutePath();

}