1

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.

  • "How to get external sdcard path in android programming?" -- you don't. You do not have access to arbitrary locations on [removable storage](https://commonsware.com/blog/2019/10/11/storage-situation-removable-storage.html). And, on Android 10 and higher, we are [losing access to most user-facing storage, in terms of filesystem APIs](https://commonsware.com/blog/2019/06/07/death-external-storage-end-saga.html). "I want to play audio files" -- then query the `MediaStore` for audio content. Use the `Uri` values to play it back. – CommonsWare Mar 19 '20 at 15:48
  • 1
    Does this answer your question? [How to get audio path to play audio file?](https://stackoverflow.com/questions/35985755/how-to-get-audio-path-to-play-audio-file) – Md Golam Rahman Tushar Mar 19 '20 at 15:52
  • Come on...https://developer.android.com/training/data-storage – Bruno Mar 19 '20 at 16:05
  • 1
    Does this answer your question? [Find location of a removable SD card](https://stackoverflow.com/questions/5694933/find-location-of-a-removable-sd-card) – Hooman.AS Mar 19 '20 at 16:05

1 Answers1

0
'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