Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android “访问路径”/存储/模拟/0/log.txt“;仅在我的应用程序首次加载时被拒绝_Android_Xamarin_Xamarin.forms - Fatal编程技术网

Android “访问路径”/存储/模拟/0/log.txt“;仅在我的应用程序首次加载时被拒绝

Android “访问路径”/存储/模拟/0/log.txt“;仅在我的应用程序首次加载时被拒绝,android,xamarin,xamarin.forms,Android,Xamarin,Xamarin.forms,我正在Xamarin.forms中生成txt文件。应用程序第一次在Visual Studio中同时执行请求权限和引发异常时 System.UnauthorizedAccessException Message=Access to the path "/storage/emulated/0/log.txt" is denied. 从我第二次在手机上打开应用程序开始,它就不会请求许可(正如我在崩溃前提供的)并生成txt文件。所以它工作得很好 为什么在第一次执行时会抛出错误

我正在
Xamarin.forms
中生成
txt
文件。应用程序第一次在Visual Studio中同时执行请求权限和引发异常时

System.UnauthorizedAccessException
  Message=Access to the path "/storage/emulated/0/log.txt" is denied.
从我第二次在手机上打开应用程序开始,它就不会请求许可(正如我在崩溃前提供的)并生成txt文件。所以它工作得很好

为什么在第一次执行时会抛出错误

这是我的密码

protected override void OnStart()
{
    base.OnStart();
    if ((int)Build.VERSION.SdkInt >= 23)
    {
        if (CheckSelfPermission(Manifest.Permission.AccessFineLocation) != Permission.Granted)
            RequestPermissions(LocationPermissions, RequestId);
    }
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    if (requestCode == RequestId)
    {
        if ((grantResults.Length == 1) && (grantResults[0] == (int)Permission.Granted))
        {
            ///Premission granted by user
            string filePath = string.Empty;
            filePath = Android.OS.Environment.ExternalStorageDirectory.ToString();
            var filename = System.IO.Path.Combine(filePath, "log" + ".txt");                  
        }
        else
        {
            ///Permission denied by user
        }
    }
    else
    {
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

您使用的路径是外部存储。如果要在Xamarin.forms中使用此路径,可以使用依赖项服务

创建接口IExternalStorage:

public interface IExternalStorage
{
    string GetPath();
}
Android上的实现:请注意,
Android.OS.Environment.ExternalStorageDirectory
已被弃用

[assembly:Dependency(typeof(AndroidImplementation))]
namespace App.Droid
{
    public class AndroidImplementation: IExternalStorage
    {
        public string GetPath()
        {
           Context context = Android.App.Application.Context;
           var filePath = context.GetExternalFilesDir("");
           return filePath.Path;
        }
    }
}
然后您可以在Xamarin中使用它。表单:

var folderPath = DependencyService.Get<IExternalStorage>(). GetPath();
var folderPath=DependencyService.Get()。GetPath();
获取路径后,可以使用下面的代码创建文件并输入一些字符串

  var filePath = DependencyService.Get<IExternalStorage>().GetPath();
        var path = System.IO.Path.Combine(filePath, "log" + ".txt");
        using (var writer = File.CreateText(path))
        {
            await writer.WriteLineAsync("hello");
        }
   //path:  /storage/emulated/0/Android/data/com.companyname.app5/files/log.txt
var filePath=DependencyService.Get().GetPath();
var path=System.IO.path.Combine(filePath,“log”+“.txt”);
使用(var writer=File.CreateText(路径))
{
等待writer.WriteLineAsync(“你好”);
}
//路径:/storage/emulated/0/Android/data/com.companyname.app5/files/log.txt

代码中的文件名
ProductStatusLog
,与错误消息不同:
log.txt
@SushiHangover-我的代码仅具有更新的名称。还是同一个问题。非常感谢。