Reputation: 1025
I have a Xamarin UWP app and am trying to load a file from my current users "Documents" library.
I understand that I need to add a File Type Association declaration first. I've done this and the file icon has changed to my application icon.
In the app I'm then using the FilePicker plugin from here...
https://github.com/ArtjomP/FilePicker-Plugin-for-Xamarin-and-Windows
FileData file = await CrossFilePicker.Current.PickFile();
Byte[] data = file.DataArray;
All I do is browse to the file and select it, but the application crashes with an access violation on the second line.
I've added the following capabilities to my UWP manifest, not sure if they are event relevant anymore.
<uap:Capability Name="documentsLibrary" />
<uap:Capability Name="removableStorage" />
How do I open my files? I ideally need to use a convenient location like the documents library.
Nick.
Edit:
I've followed this article, and still no joy.
https://www.pmichaels.net/2016/11/11/uwp-accessing-documents-library/
Upvotes: 0
Views: 800
Reputation: 9990
EDIT: I reworded the answer for clarity, and to avoid some implications that @MickyD rightly pointed in comments and I don't agree with those implications either:
This seems like a bug in the package as it doesn't work according to its documentation and one possible thing to do is to submit the issue to GitHub so that the bug is resolved by the developer of the package.
The other possible thing to do is to look for the alternatives. You may try to find other similar packages and see if they work, and the alternative for which I am sure that works is to use the native functions in System.Windows.Storage
(as it is officially supported).
Upvotes: 1
Reputation: 7179
You are using a old package which most likely is no longer supported.
Try this one, https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows
It is quite simple, and also supports UWP.
Sample usage
try
{
FileData fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
return; // user canceled file picking
string fileName = fileData.FileName;
string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);
System.Console.WriteLine("File name chosen: " + fileName);
System.Console.WriteLine("File data: " + contents);
}
catch (Exception ex)
{
System.Console.WriteLine("Exception choosing file: " + ex.ToString());
}
Upvotes: 1