简体   繁体   English

Xamarin.Android上某些文件上的UnauthorizedAccessException

[英]UnauthorizedAccessException on Xamarin.Android on some files

I have some files in the Environment.SpecialFolder.Personal folder, say A and B for simplicity. 我在Environment.SpecialFolder.Personal文件夹中有一些文件,为简单起见,请说A和B。

File A was created by the app, while file B was copied in via Android Studio's Device File Explorer. 文件A是由应用创建的,而文件B是通过Android Studio的设备文件资源管理器复制的。 The app can open A, but B throws an UnauthorizedAccessException . 该应用程序可以打开A,但是B会抛出UnauthorizedAccessException

To open, after checking whether the File.Exists , I use 打开后,检查File.Exists是否使用

FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None);

File.Exists correctly returns that the files exists, but creating the new FileStream fails for B. File.Exists正确返回文件存在,但是对于B创建new FileStream失败。

I have already tried changing the user/group of the B file, as well as changing the permissions to match the info from file A. 我已经尝试过更改B文件的用户/组,以及更改权限以匹配文件A中的信息。

I am debugging this in an Android emulator with version 7.1.1 API level 25. 我正在使用版本为7.1.1 API 25级的Android模拟器进行调试。

How can I solve the problem, or at least find more information about the problem? 如何解决该问题,或者至少找到有关该问题的更多信息?

Edit: I've tried a 8.1 API level 27 with no success, but on a physical device LG D618 with 5.0 API 21 it works. 编辑:我尝试了8.1 API级别27,但没有成功,但是在具有5.0 API 21的物理设备LG D618上,它可以正常工作。

How can I make the emulator work? 如何使模拟器工作?

I think the reason is that you need to ask the permission at runtime. 我认为原因是您需要在运行时询问权限。

According to the official documentation : 根据官方文件

You declare that your app needs a permission by listing the permission in the app manifest and then requesting that the user approve each permission at runtime (on Android 6.0 and higher). 通过在应用清单中列出该许可,然后请求用户在运行时(在Android 6.0及更高版本上)批准每个许可,即可声明您的应用需要许可。

You physical device is running on Android 5.0, so it works fine. 您的物理设备正在Android 5.0上运行,因此可以正常工作。

You could refer to this blog for how to do it in xamarin. 您可以在xamarin中参考此博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM