Pelin Konaray
Pelin Konaray

Reputation: 332

How to select folder with picker in Xamarin Forms?

I have an app that is file management app like drive. I want to add sync mechanism with Xamarin Forms app.

So the sync mechanism I want is this: User will choose a folder from my app, and will choose a folder from their phone. App will sync both of them, when added a file or folder to this folders. For example: When a file is added to the selected folder (on the phone) from the app, this file will be uploaded to the remote server with the help of my app. Or When a file is added to the folder (in the app) selected from the app, this file will be downloaded from the remote server to the selected folder on the phone with the help of my app.

My app will work in the background or sync will be triggered with button in my app. (Because I know ios background restricts operations. But it will be another research subject for me.)

For this reason I am looking up a folder picker. But I didn't find any plugin. When I searched, I saw that it was said that it can be done with dependency services.

So I want to get all file system structure of phone and show it with a listview for select a sync directory. I follow this documentation for get all file system structure of phone: for ios: https://learn.microsoft.com/en-US/xamarin/ios/app-fundamentals/file-system for android: https://learn.microsoft.com/en-US/xamarin/android/platform/files/

But I stuck in ios. Because I couldn't get all directories with this commands. For example I want to select a gallery album for sync. But I couldn't access gallery album path. I am accessing with image pickers but I don't want select a file, I want to select gallery album.

I reviewed Xamarin.Essentials Media Picker and File Picker source code in github. I also reviewed Plugin.FilePicker source code in github. All of them use UIDocumentPickerViewController or UIImagePickerController. But these for selecting file. These not selecting folder.

What can I do? Thanks in advance.

Upvotes: 1

Views: 1466

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10978

For now, no FodlerPicker in Xamarin.Forms.

You could create your own custom folder picker like below. Use the TapGestureRecognizer to simulate the selecting operation to save to the specific location.

  <ListView ItemsSource="{Binding folders}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding folder}">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer  Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
                        </Label.GestureRecognizers>
                    </Label>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

You could post your requirement in GitHub. https://github.com/xamarin/Xamarin.Forms/issues

Update:

You could use the code below to enumerate the subdirectories in the current directory (specified by the "./" parameter), which is the location of your application executable.

var directories = Directory.EnumerateDirectories("./");
foreach (var directory in directories) {
  Console.WriteLine(directory);
}

You could check the sample code in File system access in Xamarin.iOS: https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system

For more details you could refer to the link below. https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW1

Upvotes: 0

Related Questions