1

I have a problem with creating folder with nuget package PCLStorage, I cannot create folder. Nothing appear inside my files folder. I,m using my device not emulator there is android version 8.0

public async Task WriteDataAsync(string filename, string data)
        {
            string folderName = "SignatureSotrage";
            IFolder folder = FileSystem.Current.LocalStorage;

            folder = await folder.CreateFolderAsync(folderName, CreationCollisionOption.ReplaceExisting);
        }

Here is a code where I run this function:

public ICommand AddCustomerCommand => new Command(async () =>
        {
            Signature = await SignatureFromStream();
            // Signature should be != null

            var customer = new Customer()
            {
                FullName = this.FullName,
                IsAccepted = this.IsAccepted,
                Birthday = this.Birthday
            };

            if(Signature != null)
            {
                customer.Image = this.Signature.ToString();
            }
            else
            {
                await Application.Current.MainPage.DisplayAlert("Błąd", "Nie wszystkie pola zostały poprawnie wypełnione", "OK");
                return;
            }
            await DependencyService.Get<IFileHelper>().WriteDataAsync("signature.txt", "this is file");
            //_context.Customers.Add(customer);
            //_context.SaveChanges();
        });
Piotr Szary
  • 63
  • 1
  • 8

2 Answers2

1

did you debug your code & check if the file/folder is actually getting created by your code or else it enters the catch block and goes with the normal flow?

Check for UserPermissions every time for reading & write permission before doing any operations on the storage. You can add the Nugget packet Plugin.Permission it handles everything for you, it adds both the permission in the manifest.

For checking user permissions always try calling CheckForStoragePermissions() before performing any operations on storage.(*DialogService is CustomDialogBox)

           if( !await CheckForStoragePermissions() ) 
            {
                DialogService.Alert("Invalid Permission", "User declined permission for this action");
                return;
            }


    private async Task<bool> CheckForStoragePermissions()
    {
        PermissionStatus storagePermissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
        if (storagePermissionStatus != PermissionStatus.Granted)
        {
            Dictionary<Permission, PermissionStatus> storagePermissionResult = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
            if (storagePermissionResult.ContainsKey(Permission.Storage))
            {
                storagePermissionStatus = storagePermissionResult[Permission.Storage];
            }
        }
        return storagePermissionStatus == PermissionStatus.Granted;
    }
Blu
  • 821
  • 4
  • 17
0

I test the sample code on GitHub. https://github.com/dsplaisted/PCLStorage

Based on my test the folder path would like:

/data/user/0/PCLStorage.Test.Android/files/

It is a internal storage. You couldn't see the files without root permission. https://learn.microsoft.com/en-us/xamarin/android/platform/files/#working-with-internal-storage

If you want to see the files in internal storage, you could use adb tool. Please refer to the way in the link. How to write the username in a local txt file when login success and check on file for next login?

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17