PCLStorage icon indicating copy to clipboard operation
PCLStorage copied to clipboard

RoamingStorage on Android/iOS gets "System.NullReferenceException: Object reference not set to an instance of an object"

Open SmartmanApps opened this issue 8 years ago • 3 comments

Hi,

Relatively new to Xamarin. Trying PCLstorage as a quick and easy way to get text files working. Have succeeded with using LocalStorage on Android/iOS/UWP, however comes up with "System.NullReferenceException: Object reference not set to an instance of an object" if I try to use RoamingStorage on Android/iOS (it does work for UWP however). I am wanting to have user-accessible files, and I'm presuming using the RoamingStorage option is the way to do it? (LocalStorage is definitely NOT user-accessible on Android - doesn't seem to matter with UWP).

SmartmanApps avatar Apr 28 '17 07:04 SmartmanApps

Hi,

i have the same problem as you. RoamingStorage always returns null on Android and IOS.

I changed

/src/PCLStorage.FileSystem.Desktop/DesktopFileSystem.cs

public IFolder RoamingStorage
{
    get
    {
#if ANDROID || IOS
        return null;
#else
        //  SpecialFolder.ApplicationData is not app-specific, so use the Windows Forms API to get the app data path
        //var roamingAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        var roamingAppData = System.Windows.Forms.Application.UserAppDataPath;
        return new FileSystemFolder(roamingAppData);

#endif
    }
}

to

public IFolder RoamingStorage
{
    get
    {
#if ANDROID
        var roamingAppData = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
#elif IOS
        return null;
#else
        //  SpecialFolder.ApplicationData is not app-specific, so use the Windows Forms API to get the app data path
        //var roamingAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        var roamingAppData = System.Windows.Forms.Application.UserAppDataPath;
#endif
        return new FileSystemFolder(roamingAppData);
    }
}

and it seems to work. Since i don't have a mac, i can't change the IOS version. Maybe someone can come up with an IOS solution.

jdillenkofer avatar Jun 23 '17 09:06 jdillenkofer

Thanks, but how do I navigate to that page you have mentioned? (I haven't updated an external package before - I've been looking around, but can't see where this is, in VS2017 or in Windows) If I can find it then I can have a go at the iOS version (since I do have access to a Mac)

SmartmanApps avatar Jun 26 '17 07:06 SmartmanApps

Isn't RoamingStorage the place where you put data to be stored in the 'cloud; or somewhere as part of the user profile and you expect to be restored if the user installs your app in a different machine? If so, it doesn't look to me that the https://developer.xamarin.com/api/property/Android.OS.Environment.ExternalStorageDirectory/ is the right place. Just my 2 cents,

xavier-rigau avatar Jun 26 '17 17:06 xavier-rigau