Xamarin.Android:System.UnauthorizedAccessException:对路径的访问被拒绝 [英] Xamarin.Android : System.UnauthorizedAccessException: Access to the path is denied

查看:56
本文介绍了Xamarin.Android:System.UnauthorizedAccessException:对路径的访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是 Android API 30.我的应用程序正在存储日志文件并在/storage/emulated/0/SpecialDir/"位置进行数据库备份.现在,当我的应用程序之前运行良好时,我面临访问被拒绝的问题.我大致了解了范围存储,并了解到我们有一些托管位置可以相应地存储我们的数据.即音频、视频、图像和下载

I am targeting Android API 30. My app was storing log file and taking database backup in location "/storage/emulated/0/SpecialDir/". Now I am facing access denied issue while my app was workinng fine previously. I got an overview about scoped storage and came to know that we have some managed locaitons where we can store our data accordingly. i.e Audio, Video, Images, and Download

  • 我的问题是,对于以前将文件保存在/storage/emulated/0/SpecialDir/"上的现有应用,有什么解决方案.

谁能指导我我该怎么做.

Can anyone please guide me what should i do.

string dir = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "LogFolder");
if (Directory.Exists(dir))
{
    return Path.Combine(dir, "MyLogFile.txt");
}
try
{
   string newDirectory = Directory.CreateDirectory(dir).FullName;
   path = Path.Combine(newDirectory, "MyLogFile.txt");
   System.IO.File.WriteAllText(path, "This is some testing log.");
}
catch (Exception ex)
{
   string msg = ex.Message;
}

上面的代码用于创建LogFolder"(如果不存在)和MyLogFile".我需要进行哪些更改才能使其与 Android 10 兼容.谢谢

The above code is used to make 'LogFolder' if not exist and 'MyLogFile' as well. What changes do i needed to make it compatiable to Android 10. Thankyou

推荐答案

在 Android 10 中,Google 为外部存储引入了一项新功能.它的名字是Scoped Storage.Google 正式将其翻译为分区存储,或 Scoped Storage.目的是限制程序可以对外部存储中的公共目录执行的操作.分区存储对内部存储私有目录外部存储私有目录都没有影响. 总之,在Android 10中,私有目录读写没有变化,并且您仍然可以在没有任何权限的情况下使用 File 集.对于公共目录的读写,你必须使用 MediaStoreSAF(存储访问框架)提供的 API,这意味着你不能再使用 File 集随意操作公共目录.

In Android 10, Google has introduced a new feature for external Storage. Its name is Scoped Storage. Google officially translates it as partitioned Storage, or Scoped Storage.The intent is to limit what programs can do with public directories in external storage. Partitioned storage has no effect on either the internal storage private directory or the external storage private directory.In short, in Android 10, there is no change to private directory reads and writes, and you can still use the File set without any permissions. For reading and writing to public directories, you must use the API provided by MediaStore or the SAF (storage access framework), which means you can no longer use the File set to manipulate public directories at will.

如果您将 targetSdkVersion 设置为 29 以上,您可以尝试将以下代码添加到您的 AndroidManifest 中.然后您可以访问 文件和以前一样.

If you set targetSdkVersion above 29,you could try to add below codes into your AndroidManifest.Then you could access the File as before.

<manifest ... >
   <application android:requestLegacyExternalStorage="true" ... >
       ...
   </application>
</manifest>

更新(您可以尝试将其用于公共外部存储):

Update (you could try this for public external storage ):

var path = Android.OS.Environment.GetExternalStoragePublicDirectory("LogFolder").AbsolutePath;
Java.IO.File file = new Java.IO.File(path);


if (!file.Exists())
   {
     file.Mkdirs();
   }
try
   {
     FileWriter fw = new FileWriter(path + Java.IO.File.Separator + "MyLogFile.txt");
     fw.Write("This is some testing log.");
     fw.Close();
   }
catch (Exception ex)
   {

      string msg = ex.Message;
   }

Android 11 更新:

在您的 AndroidManifest 中添加 MANAGE_EXTERNAL_STORAGE 权限.

add MANAGE_EXTERNAL_STORAGE permission in your AndroidManifest.

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

在您的活动中:

  if (Environment.IsExternalStorageManager)
        {
            var path = Android.OS.Environment.GetExternalStoragePublicDirectory("LogFolder").AbsolutePath;
            Java.IO.File file = new Java.IO.File(path);

            if (!file.Exists())
            {
                file.Mkdirs();
            }
            try
            {
                FileWriter fw = new FileWriter(path + Java.IO.File.Separator + "MyLogFile.txt");
                fw.Write("This is some testing log.");
                fw.Close();
            }
            catch (Exception ex)
            {

                string msg = ex.Message;
            }
   
        }
        else
        {
         
            StartActivityForResult(new Intent(Settings.ActionManageAllFilesAccessPermission), 0);
        }
 

这篇关于Xamarin.Android:System.UnauthorizedAccessException:对路径的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆