Full working Sample
Hi,
I tried to replace PCLStorage with PCLExt.FileStorage and found the JSON and Log examples, but I didn't get it working.
I want to do something like:
var rootFolder = new ApplicationRootFolder();
await rootFolder.CreateFolderAsync(FileStoreConstants.TmpFolderName, CreationCollisionOption.OpenIfExists);
But I always get a NullReference Exception on rootFolder.
I'm using a Xamarin.Forms Solution with UWP, Android, iOS and .netstandard2.0 libararies (refer to the latest Prism Template Pack). I also tried to instantiate the root folder directly in the several platform project without success. The library is installed via nuget in the main project, the three platform projects (UWP, Android, iOS) and one or two libraries.
What I'm doing wrong? Is it possible to get a full working sample solution?
ApplicationRootFolder is not supported on all platforms. It's working on iOS, but not Android or UWP. The UWP should work in theory, but it's not supported from the code right now, I'm investigating some strange permission issues
I am using something like:
_rootFolder = new DocumentsRootFolder()
and I'm getting a path I can work with. Maybe that can help.
Same problem here on UWP - Android and iOS are working without problems. Using Version 1.3.1 from NuGet.
We have a pull request that I hope should fix UWP
Should be working with 1.3.2 now
1.3.2 crashes on Android and throws an UnauthorizedAccessException on UWP. :-(
Can you provide the code that crashes on Android?
Sure, here - crash in the 2nd line.
var rootFolder = new ApplicationRootFolder(); var folder = await rootFolder.CreateFolderAsync(MetaService, CreationCollisionOption.OpenIfExists); if ( !await this.CheckFileExistsAsync(folder, ServiceRequestDraftJson) ) return null;
var draftFile = await folder.GetFileAsync(ServiceRequestDraftJson); var json = await draftFile.ReadAllTextAsync();
ApplicationRootFolder is not supported on all platforms.
It's working on iOS, but not Android or UWP
@Aragas thanks for die fast reply. Which IFolder-Implementation is supported on all platforms?
I guess the DocumentsRootFolder and the LocalRootFolder
DocuementsRootFolder works with 1.3.1 on Android but 1.3.2 crashes.
Interesting. Code to reproduce?
@Aragas Same code as yesterday - only ApplicationRootFolder replaced by DocumentsRootFolder.
Strange issue. Should work in both versions
Use LocalRootFolder to store your app data or RoamingRootFolder (in case you need roaming) or TempRootFolder (for temp files). You can't get access to DocumentsRootFolder msdn without special permission. I'm not sure about the ApplicationRootFolder for UWP I believe it's only for reading.
When I use LocalRootFolder, it gives the path as, /storage/emulated/0/Android/data/com.companyname.AppName
But generally all the apps folders are under /storage/emulated/0 How to get access to this root folder? I want to create my app folder in this root folder.
Just as example if I use MusicRootFolder, it properly gives /storage/emulated/0/Music path.
@imhrushi you could try new FolderFromPath("/storage/emulated/0")
Thanks Aragas, I was able to use that folderfrompath. But now when I try to create a folder using that class object, it gives me error access denied. I suppose I have to check and ask for the runtime storage permissions? the code on the net for this needs Xamarin.Android.Support.Compat package and in turn that needs monoandroid framework. Now my xamarin forms in visual studio does not have this framework. Do I need to download it and install in order to do this all stuff? In the localrootfolder I have mentioned above, I am able to create a folder. It does not give access denied error. So I am curious.
@imhrushi check first if your app has read&write permissions for external storage. If you have, then maybe you need to ask for the actual permissions from the user. Check this library for a sample https://github.com/jamesmontemagno/PermissionsPlugin/blob/master/README.md