Rakesh
Rakesh

Reputation: 2790

Android - Xamarin UnauthorizedAccessException: Access to the path "/storage/sdcard0/ denied

I am trying to access one file from the device memory. below was the code

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 file also changes as

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

But I am getting error UnauthorizedAccessException: Access to the path "/storage/sdcard0/ denied

My Android version is Jelly bean

Upvotes: 0

Views: 601

Answers (2)

Rakesh
Rakesh

Reputation: 2790

Found out the issue finally

line android:maxSdkVersion="18" making the issue I have removed it and got worked

Upvotes: 0

Kamil Kulach
Kamil Kulach

Reputation: 519

Use Android.OS.Environment.ExternalStorageDirectory.Path instead of Android.OS.Environment.ExternalStorageDirectory.AbsolutePath that fixed it for me

Upvotes: 0

Related Questions