Android-Xamarin UnauthorizedAccessException:访问路径“ / storage / sdcard0 /拒绝

问题描述 投票:0回答:2

我正在尝试从设备存储器访问一个文件。下面是代码

string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath);
string Mp3 = Path.Combine(directory, "root.txt");
Toast.MakeText(Activity , "directory: " + directory, ToastLength.Long).Show();
if (File.Exists(Mp3))
{
    string applicationFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "STRACK");
    File.Copy(Mp3, applicationFolderPath+ "/root.txt");
}
else
{
    Toast.MakeText(Activity, "directory: " + directory, ToastLength.Long).Show();
} 

AndroidManifest文件也更改为

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />

但是我收到错误UnauthorizedAccessException: Access to the path "/storage/sdcard0/ denied

我的Android版本是Jelly bean

c# io xamarin.android android-4.2-jelly-bean
2个回答
0
投票

使用Android.OS.Environment.ExternalStorageDirectory.Path代替Android.OS.Environment.ExternalStorageDirectory.AbsolutePath为我解决的问题


0
投票

终于找到了问题

android:maxSdkVersion="18"造成的问题,我已将其删除并开始工作

© www.soinside.com 2019 - 2024. All rights reserved.