'How to get external sdcard path in android programming?

I want to play audio files from memory card.

How to get external sdcard path in android programming?

Unfortunately, every android phone model has a different path.for example:

Galaxy S3 android 4.4: memory card exists in the path: "/mnt/extSdCard/"

Honor 7c android 8: memory card exists in the path: "/mnt/ext_sdcard/"

I couldn't find the path in Nokia 5 with android 9 and Xiaomi Redmi 3s with android 6.

 try {

File root = Environment.getExternalStorageDirectory();
String audioPath = root.getAbsolutePath() + "/telavatquran/naba.mp3";

            FileInputStream fis = new FileInputStream(audioPath);

            fd = fis.getFD();

            if (fd != null) {
                player.setDataSource(fd);
                player.prepare();
                player.start();

            } else if (new File("/mnt/sdcard/external_sd/").exists()) {
                seekUpdation();
                player.setDataSource("/mnt/sdcard/external_sd/telavatquran/naba.mp3/");
                player.prepare();
                player.start();

            } else if (new File("/storage/extSdCard/").exists()) {
                seekUpdation();
                player.setDataSource("/storage/extSdCard/telavatquran/naba.mp3/");
                player.prepare();
                player.start();

            } else if (new File("/mnt/extSdCard/").exists()) {
                seekUpdation();
                player.setDataSource("/mnt/extSdCard/telavatquran/naba.mp3/");
                player.prepare();
                player.start();

            } else if (new File("/mnt/sdcard/external_sd/").exists()) {
                seekUpdation();
                player.setDataSource("/mnt/sdcard/external_sd/telavatquran/naba.mp3/");
                player.prepare();
                player.start();

            } else if (new File("storage/sdcard1/").exists()) {
                seekUpdation();
                player.setDataSource("storage/sdcard1/telavatquran/naba.mp3/");
                player.prepare();
                player.start();

            } else if (new File("/mnt/ext_sdcard/").exists()) {
                seekUpdation();
                player.setDataSource("/mnt/ext_sdcard/telavatquran/naba.mp3/");
                player.prepare();
                player.start();

            }
} catch (Exception e) {

                }

I’d appreciate your cooperation.



Solution 1:[1]

'this is a b4a code I used


Sub GetRootExternalSDCard() As String

Dim rp As RuntimePermissions
Dim Swriteablefolder  As List
Dim tempstr As String
Dim x As Int


Swriteablefolder=rp.GetAllSafeDirsExternal("")

'   For x=0 To Swriteablefolder.Size-1
'       Log(Swriteablefolder)
'   Next
tempstr=Swriteablefolder.Get(1)

x=tempstr.IndexOf("/Android")
Return tempstr.SubString2(0,x)

End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Devendra Karanjit