1

I am currently working on an app, that goes through your phone and lists all available MP3 files. I managed to get this done and search for everything on the internal storage, but didnt manage to find a way using the envoirment to get to the sd card, when one is installed. This is my code - u will see a missing part when SD card is TRUE. Can you complete it?

    public List<string> ReturnPlayableMp3(bool sdCard)
    {
        List<string> res = new List<string>();
        string phyle;

        if(sdCard)
        {
            // missing 
        }
        else
        {
            try
            {
                var path1 = Android.OS.Environment.ExternalStorageDirectory.ToString();
                var mp3Files = Directory.EnumerateFiles(path1, "*.mp3", SearchOption.AllDirectories);

                foreach (string currentFile in mp3Files)
                {
                    phyle = currentFile;
                    res.Add(phyle);
                }
            }
            catch (Exception e9)
            {
                Toast.MakeText(ApplicationContext, "ut oh\n" + e9.Message, ToastLength.Long).Show();
            }
        }


        return res;
    }


}

It would need to return the exact same thing as it does for the internal storage only this time for the sd card. Right now, what is beeing returned is:

""/storage/emulated/0""

I hope you can help me. Thank you!

SO I found the place it is: /storage/05B6-2226/

But the digits refer to only MY sd card. How do I get this path programatically?

innomotion media
  • 862
  • 11
  • 30

4 Answers4

2

Take a look at these methods:

Returns the absolute path to the directory on the primary external filesystem (that is somewhere on Environment.ExternalStorageDirectory) where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media.

Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

Michał Żołnieruk
  • 2,095
  • 12
  • 20
0

I've been searching for a couple of days with a lot of solutions that just ended up giving you the 'external' built in storage. Finally found this solution for the 'removable' SD Card and wanted to post it here in case someone else is still looking.

How to write on external storage sd card in mashmallow in xamarin.android

ImBunky
  • 1
  • 2
  • While this might theoretically answer the question, it would be [much better](https://meta.stackexchange.com/q/8259) to include the essential parts of the answer here and provide the link for reference. – Joey May 01 '19 at 01:14
0
//Get the list of External Storage Volumes (E.g. SD Card) 
Context context = Android.App.Application.Context;
var storageManager = (Android.OS.Storage.StorageManager)context.GetSystemService(Context.StorageService);
var volumeList = (Java.Lang.Object[])storageManager.Class.GetDeclaredMethod("getVolumeList").Invoke(storageManager);

List<Java.IO.File> ExtFolders = new List<Java.IO.File>();

//Select the Directories that are not Emulated
foreach (var storage in volumeList)
{

    Java.IO.File info = (Java.IO.File)storage.Class.GetDeclaredMethod("getDirectory").Invoke(storage);
    if ((bool)storage.Class.GetDeclaredMethod("isEmulated").Invoke(storage) == false && info.TotalSpace > 0)
    {
        //Get Directory Path
        Console.WriteLine(info.Path);
    }
}
Elikill58
  • 4,050
  • 24
  • 23
  • 45
0

Just wanna share my answer, where I have get the extStorages Path and I use this method in my simple file browser app.

public static string[] GetRemovableStorages()
{
    List<string> extStorage = new List<string>();
    //If this throws exception
    string storageDir = (string)Environment.StorageDirectory;
    //Try this
    string storageDir = Directory.GetParent (Environment.ExternalStoragePublicDirectory).Parent.FullName;
    string[] directories = Directory.GetDirectories(storageDir);
        
    foreach(string dir in directories)
    {
        try
        {
            var extStoragePath = new Java.IO.File(dir);
            bool isRemovable = Environment.InvokeIsExternalStorageRemovable(extStoragePath);
                
            if(isRemovable) extStorage.Add(extStoragePath.AbsolutePath);
            else return null;
        }   
        catch
        {
        
        }
    }
        
    return extStorage.ToArray();
}

Elikill58's answer throws exception no such method "getDirectory" in my case but I recommend Elikill58's answer