0

I'm trying to get all the available media files -Or files with specific extensions- in the device -Either Android or IOS- but I can't find a way to do this, so instead I was trying to get all the device storages and I'll iterate through them with my own code to get the files I need.

However, I can't find a way to get the storages paths as well, but I know I get the main storage path in Android using Android.OS.Environment.ExternalStorageDirectory.AbsolutePath so maybe I can make an interface for the same for IOS, but still, that gets the main storage only, not any attached USB or Memory Cards...

So any help would be appreciated in either approaches. Thanks in advance.

Mohamed Ashraf
  • 181
  • 3
  • 19

1 Answers1

3

Android:

Android groups the filesystem into two different types of storage: Internal Storage, External Storage.

Internal Storag: https://learn.microsoft.com/en-us/xamarin/android/platform/files/#working-with-internal-storage

You could use System.Environment.GetFolderPath() to retrieve internal storage directory like below:

/data/user/0/com.companyname/files

External Storage: https://learn.microsoft.com/en-us/xamarin/android/platform/files/external-storage?tabs=windows

The directory of the private external files would be:

/storage/emulated/0/Android/data/com.companyname.app/files/

You could get it via dependency service.

DependencyService: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app- fundamentals/dependency-service/introduction

Create an interface IExternalStorage:

public interface IExternalStorage
{
    string GetPath();
}

The implementation on the Android:

[assembly:Dependency(typeof(AndroidImplementation))]
namespace App.Droid
{
    public class AndroidImplementation: IExternalStorage
    {
        public string GetPath()
        {
           Context context = Android.App.Application.Context;
           var filePath = context.GetExternalFilesDir("");
           return filePath.Path;
        }
    }
}

Then you could use it in Xamarin.Forms:

var folderPath = DependencyService.Get<IExternalStorage>(). GetPath();

IOS:

Each iOS app has its own sandbox directory. https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

Generally, we store user's data in the Documents folder. And we could access its path on Forms directly:

var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

Please note: IOS only could access photo gallery and iCloud folder.

File system access in Xamarin.iOS: https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • I don't think you got my point, what I'm seeking is to retrieve the main directories (Storage, SD Card, USB, etc.) in order to iterate all its files and load them into my custom made media player. Not to get the application folder. – Mohamed Ashraf Apr 06 '20 at 17:17
  • USB, SD card are all external storage. You could check the part of external storage. – Wendy Zang - MSFT Apr 07 '20 at 09:11
  • I know that it is external storage, and the part of external storage doesn't tell me how to get a list (or array) of paths for each storage connected to the device... – Mohamed Ashraf Apr 07 '20 at 09:37
  • ListFiles() will get the files in the main external storage, again this won't get me the files in the SD Card nor the USB drive... – Mohamed Ashraf Apr 09 '20 at 13:00
  • 1
    You could check the link below. It would be helpful. https://stackoverflow.com/questions/9340332/how-can-i-get-the-list-of-mounted-external-storage-of-android-device – Wendy Zang - MSFT Apr 14 '20 at 08:09
  • I'll give that a shot and let you know the result, but it looks promising... – Mohamed Ashraf Apr 15 '20 at 01:46