1

I am trying to play an mp3 file stored in my SDcard. I am not using an emulator but a Samsung Galaxy S3 with cyanogenmod 13 android 6.0.

First I tried with

Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/gato.mp3")

but the

Environment.getExternalStorageDirectory().getPath() gets me to /storage/emulated/0.

With the file manager in my phone I see 3 memory storage places:

  1. Internal storage is located in the /storage/emulated/0.
  2. sdcard1 is in /storage/FE97-A11B
  3. secure storage apart.

Which are different from what I see exploring from my computer (only sdcard1 and internal storage).

I couldnt make it work so I put the file "gato.mp3" in both internal and sdcard1, and I can check is there. But with

Environment.getExternalStorageDirectory().getPath()+"/gato.mp3"

which gives me the path /storage/emulated/0/gato.mp3, which should get me to the file, and also trying to set the path manually to /storage/FE97-A11B/gato.mp3, the mediaplayer created with the uri is null, and no file is found. Same if I try the paths I see in the computer explorer.

Anybody has any idea on why it cannot find the file? I have tried removing "/" at the beggining, adding "mnt" to see if i was missing something in the path, but nothing seem to work.

Thank you for your time!

My code to check is just a function

public void ejecutar(View v) {

    Uri datos = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/gato.mp3");

    MediaPlayer mp = MediaPlayer.create(this, datos);

    if (mp != null) {
        mp.start();
    }
    else{ tv1.setText(datos.toString());}

where tv1 is a textview so i can see which path the uri is pointing to.

Oscarb
  • 11
  • 2

1 Answers1

0

I am trying to play an mp3 file stored in my SDcard

You do not have access to arbitrary filesystem locations on removable storage.

Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/gato.mp3")

Never use concatenation when assembling paths. And, I am not certain if create(Context, Uri) works with file Uri schemes. I recommend that you use:

File media=new File(Environment.getExternalStorageDirectory(), "gato.mp3");

and then use setDataSource(media.getAbsolutePath()). You will also need the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission, which also means that you need to deal with runtime permissions.

but the Environment.getExternalStorageDirectory().getPath() gets me to /storage/emulated/0

That is a typical path for what the Android SDK refers to as external storage, and you refer to as internal storage in your question. What the SDK refers to as internal storage is something else.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ok, i see thats the main path. I have created a File media as you proposed. I print media.toString(), and indeed it poitns at /storage/emulated/0/gato.mp3, where my file is. Then I am not very sure if im doing right. I created a Mediaplayer mp = new Mediaplayer(); After I set: mp.setDataSource(media.getAbsolutePath()) Android Studio then need me to wrap it in a try {} catch {}, and after i call mp.start() and nothing happens. Did I forgot to initialize something on my MediaPlayer? Thank you – Oscarb Jan 09 '17 at 16:49
  • @CommonsWare do you mind having a look at a question I posted - https://stackoverflow.com/questions/52830390/null-when-trying-to-parse-uri-from-string-path – ClassA Oct 17 '18 at 09:33