OpenReadAsync returns ex Access to the path is denied with sharing target xamarin ios

130 Views Asked by At

I'm implementing sharing target in my Xamarin.iOS project.

All the described behavior works on simulator, but fails on real device.

The OpenReadAsync function is already used and working when I select the file via the FilePicker (file type is FileResult):

var file = await FilePicker.PickAsync(allowedFileTypes);
using Stream stream = await file.OpenReadAsync();

But if I try to create the FileResult manually with the path I get from the sharing target:

FileResult file = new FileResult(filePath);
using Stream stream = await file.OpenReadAsync();

I get those 2 exceptions:

  • Shared from gallery:
{System.IO.DirectoryNotFoundException: Could not find a part of the path "/var/mobile/Media/PhotoData/OutgoingTemp/EF8C049F-C529-44DB-B6FD-3CC6B3ED189D/IMG_0003.JPG".
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAc…}
  • Shared from Files:
{System.UnauthorizedAccessException: Access to the path "/private/var/mobile/Containers/Shared/AppGroup/0B20B6FA-C27F-4487-9719-D877CAD58452/File Provider Storage/Downloads/IMG_0003.JPG" is denied.
  at System.IO.FileStream..ctor (System.String path, System.IO.…}

Same behavior if I use System.IO.File.ReadAllBytes(filePath)

The manually created file obviously miss some access right and the simulator isn't that restrictive as a real device.

But I cannot figure out how to read bytes from a file from just the path given by the sharing extension without using the FilePicker which gives the needed access rights.

I also tried doing the conversion to bytes on the iOS side using

NSUrl nSUrl = new NSUrl(cleanUri);
nSUrl.StartAccessingSecurityScopedResource();
var data = NSData.FromUrl(nSUrl);

But NSData.FromUrl always returns null.

0

There are 0 best solutions below