Unable to create file or folder with either PCLStorage or IsolatedStorage APIs.

joeldeanjoeldean JMMember ✭✭

m developing an Android app using the Xamarin platform and Im trying to create a folder on my local storage but I have had no success.

First I tried using the System.IO namespace through a FileManager class created by Xamarin Forms Labs. A snippet of the functions I was passing the path of "/storage/emulated/0/`".

        public void CreateDirectory(string path)
        {
            this.isolatedStorageFile.CreateDirectory(path);
        }

        public Stream OpenFile(string path, FileMode mode, FileAccess access)
        {
            return (Stream)this.isolatedStorageFile.OpenFile(path, (System.IO.FileMode)mode, (System.IO.FileAccess)access);
        }

This didn't work so I then opted to using PCLStorage a library for cross platform file operations. I tried this code.

IFolder rootFolder = FileSystem.Current.LocalStorage;
folder = await rootFolder.CreateFolderAsync("wakesocial",
                CreationCollisionOption.OpenIfExists);

Didn't work. I navigated to the root of the internal storage and I didn't see the folder.

So the error doesn't seem to be occurring because of the libraries in use but something specific to Android. I have read and write to external storage in manifest. So the question is. Does an app have permission to create a file or folder at the root level of the storage device or does it have to create it at particular location such as in Android/data/packagename

Also used local storage still no files are created.

IFolder rootFolder =FileSystem.Current.LocalStorage;
folder =rootFolder.CreateFolderAsync("media",
CreationCollisionOption.OpenIfExists).Result;

Answers

  • JasonJensenJasonJensen USUniversity

    I can't figure it out either.. I mean seriously.. saving a file is now rocket science!

  • Gigex42Gigex42 USMember ✭✭✭✭

    Did you try saving some file with text and open it after to see if it really saved it?

    I dont know if you just simply can navigate to the folder to see if it was created.

    Using this code just works fine on all platforms..

    private static readonly IFolder _rootFolder = FileSystem.Current.LocalStorage;

    public static async Task<bool> SaveToLocalStorage(string path, string content)
            {
                try
                {
                    IFile file = await _rootFolder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);
                    await file.WriteAllTextAsync(content);
    
                    return true;
                }
                catch (Exception ex)
                {
                    ErrHandler(ex);
                    return false;
                }
            }
    
  • thedeethedee USMember ✭✭

    I am having the same issues. Did you ever get this figured out?

Sign In or Register to comment.