Android - Xamarine UnauthorizedAccessException:访问路径" / storage / sdcard0 / denied

时间:2017-07-17 07:19:35

标签: c# io xamarin.android android-4.2-jelly-bean

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

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版本是果冻豆

2 个答案:

答案 0 :(得分:0)

使用     Android.OS.Environment.ExternalStorageDirectory.Path 代替     Android.OS.Environment.ExternalStorageDirectory.AbsolutePath 为我修好了

答案 1 :(得分:0)

最终找到问题

export class AuthService { // is authenticated flag subject isAuthenticatedSubject = new Subject<boolean>(); login(username: string, password: string): void { this.documentumService.login( username, password ) .subscribe( data => { // store user details in local storage to keep user logged in between page refreshes localStorage.setItem( this.tokenLabel , JSON.stringify({ token: this.documentumService.getAuthToken() })); // edit isAuthSubject this.isAuthenticatedSubject.next(true); // store response json this.authResponse = data; }, err => { this.isAuthenticatedSubject.next(false); }, null ); } export class LoginComponent { login(): void { this.loading = true; this.authenticationService.login(this.model.username, this.model.password); this.authenticationService.isAuthenticatedSubject .subscribe( data => { if (data) { this.router.navigate([this.returnUrl]); } else { this.loading = false; } }, error => { // this.alertService.error(error); this.loading = false; }); } 提出问题我已删除它并开始工作