从Xamarin在内部存储上写入文件时权限被拒绝

hmtdttj4  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(161)

我尝试做以下事情:

var photo = await MediaPicker.CapturePhotoAsync();
        string maindir = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "Pictures");
        string filename = System.DateTime.Now.ToString("yyyyMMdd_HHmmss") + System.IO.Path.GetExtension(photo.FileName);
        var newFile = Path.Combine(maindir, filename);
        Directory.CreateDirectory(newFile);
        using (var stream = await photo.OpenReadAsync())
        {
            using (var newStream = File.OpenWrite(newFile))
            {
                await stream.CopyToAsync(newStream);
            }
        }

字符串
通过使用Xamarin.Essentials.FileSystem.AppDataDirectory,我应该在 * 内部存储 * 中获得一个文件夹(顺便说一句,我还提供了外部存储写入权限,但这不应该是相关的-它没有解决这个问题)。其余的代码几乎是从Xamarin.Essentials文档逐字逐句。
在File.OpenWrite行中,我得到一个UnauthorizedAccessException,说“拒绝访问路径'/data/user/0/com.companyname.peerpeek/files/Pictures/20231115_154707.jpg'”。
从我所读到的一切来看,这不应该发生。那么:这是一个bug还是我错过了一些明显的东西?
为了更好地衡量:我在模拟Pixel 5(Android 13)上运行它时以及在我自己的手机(Android 12)上调试时都会遇到这个错误。
[编辑]按要求:这里是完整的例外:

System.UnauthorizedAccessException: Access to the path '/data/user/0/com.companyname.peerpeek/files/Pictures/20231115_163542.jpg' is denied.
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x000e7] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:196 
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:91 
  at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at System.IO.File.OpenWrite (System.String path) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:271 
  at PeerPeek.MainPage.LoadPhotoAsync (Xamarin.Essentials.FileResult photo) [0x00187] in C:\Users
icksa\source\repos\PeerPeek\PeerPeek\PeerPeek\MainPage.xaml.cs:88

arknldoa

arknldoa1#

System. out. println();
这应该是:

Directory.CreateDirectory(mainDir);

字符串
现在无法创建该文件,因为已经存在同名目录。
事实上,诉讼被驳回。

slhcrj9b

slhcrj9b2#

您可以在不使用Directory.CreateDirectory(newFile);的情况下拍摄照片。它将存储在本地存储中。

var photo = await MediaPicker.CapturePhotoAsync();
  string maindir = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "Pictures");
  string filename = System.DateTime.Now.ToString("yyyyMMdd_HHmmss") + System.IO.Path.GetExtension(photo.FileName);
  var newFile = Path.Combine(maindir, filename);
  using (var stream = await photo.OpenReadAsync())
  using (var newStream = File.OpenWrite(newFile))
  await stream.CopyToAsync(newStream);

字符串

相关问题