Failed to create folder / file on Android file system using PCLStorage or Xamarin Forms Labs

I am developing an Android application using the Xamarin platform and Im trying to create a folder on my local storage, but I have not been successful.

First I tried to use the System.IO namespace through the FileManager class created by Xamarin Forms Labs. A fragment of the functions that I passed along the path "/ 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 did not work, so I decided to use the PCLStorage library to work with files in cross-platform files. I tried this code.

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

Does not work. I went to the root of the internal storage and I did not see the folder.

So the error does not seem to be due to the use of libraries, but something special for Android. I read and write to the external storage in the manifest. So the question is. Does the application have permission to create a file or folder at the root level of the storage device or must it be created in a specific place, for example, in Android / data / packagename

+8
android c # xamarin
source share
3 answers
 string folderPath = Environment.ExternalStorageDirectory.AbsolutePath; //Android public async Task PCLGenaratePdf(string folderPath ) { IFolder rootFolder = await FileSystem.Current.GetFolderFromPathAsync(folderPath ); IFolder folder = await rootFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists); IFile file = await folder.CreateFileAsync("file.pdf", CreationCollisionOption.ReplaceExisting); } 
+3
source share

In Android, the folder with the local PCL storage corresponds to the special folder “My Documents” .

I'm not sure which path you are trying to create in the folder, but if you know the path and want to use the PCL repository, you can use FileSystem.Current.GetFolderFromPathAsync to get the PCL IFolder repository corresponding to the existing folder.

+1
source share

After many attempts, I solved this problem with this

  string rootPath = Android.App.Application.Context.GetExternalFilesDir(null).ToString(); var filePathDir = Path.Combine(rootPath, "Folder"); if (!File.Exists(filePathDir)) { Directory.CreateDirectory(filePathDir); } 

The created folder is accessible via adb @ / sdcard / Android / Data / Folder or something like this or in the device’s memory @ / Android / data / <AppName → / files / Folder

0
source share

All Articles