Xamarin 表单:相机和图库在 android 中不工作,显示权限异常
Xamarin forms: Camera and gallery are not working in android, showing permission exception
在我的项目中加载相机和画廊时出现以下异常。
相机
Exception:>Plugin.Media.Abstractions.MediaPermissionException: Camera permission(s) are required.
at Plugin.Media.MediaImplementation+d__17.MoveNext () [0x0009
图库
Does not have storage permission granted, requesting.01-02 15:16:08.566 I/mono-stdout(12958): Does not have storage permission granted, requesting.
Storage permission Denied.
01-02 15:16:08.567 I/mono-stdout(12958): Storage permission Denied.[0:] Exception:>Plugin.Media.Abstractions.MediaPermissionException: Storage permission(s) are required.
at Plugin.Media.MediaImplementation+d__16.MoveNext () [0x00091] in <5939e997f291496f805023da28f3a447>:0
--- End of stack trace from previous location where exception was thrown ---
我已经在清单文件中添加了所有权限。
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在主活动中添加了以下代码:
[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
清单文件中的提供商详细信息:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="{packagename}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
</paths>
相机和图库代码:
public async void OpenMygallery()
{
try
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
ShowAlert("No photos available.");
return;
}
_mediaFile = await CrossMedia.Current.PickPhotoAsync();
if (_mediaFile == null)
return;
tweetPicture.Source = ImageSource.FromStream(() =>
{
return _mediaFile.GetStream();
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
}
}
public async void OpenMyCamera()
{
try
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
ShowAlert("No camera available.");
return;
}
_mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
AllowCropping = true
});
if (_mediaFile == null)
return;
tweetPicture.Source = ImageSource.FromStream(() =>
{
return _mediaFile.GetStreamWithImageRotatedForExternalStorage();
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
}
}
错误信息:
插件版本号:4.0.1.5
测试设备:红米 Note 5 Pro
VS 版本:15.9.3
Xamarin 版本:3.4.0.1008975
任何人都可以为此提出解决方案吗?
在 manifest/info.plist 文件中设置权限是第一步,但用户还需要授予这些权限。
您始终可以添加权限检查以确保用户确实允许该应用访问 camera/location/photos/whatever。
查看James Montemagno's Permissions Plugin权限检查和请求。
在我的项目中加载相机和画廊时出现以下异常。
相机
Exception:>Plugin.Media.Abstractions.MediaPermissionException: Camera permission(s) are required. at Plugin.Media.MediaImplementation+d__17.MoveNext () [0x0009
图库
Does not have storage permission granted, requesting.01-02 15:16:08.566 I/mono-stdout(12958): Does not have storage permission granted, requesting. Storage permission Denied. 01-02 15:16:08.567 I/mono-stdout(12958): Storage permission Denied.[0:] Exception:>Plugin.Media.Abstractions.MediaPermissionException: Storage permission(s) are required. at Plugin.Media.MediaImplementation+d__16.MoveNext () [0x00091] in <5939e997f291496f805023da28f3a447>:0 --- End of stack trace from previous location where exception was thrown ---
我已经在清单文件中添加了所有权限。
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在主活动中添加了以下代码:
[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
清单文件中的提供商详细信息:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="{packagename}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
</paths>
相机和图库代码:
public async void OpenMygallery()
{
try
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
ShowAlert("No photos available.");
return;
}
_mediaFile = await CrossMedia.Current.PickPhotoAsync();
if (_mediaFile == null)
return;
tweetPicture.Source = ImageSource.FromStream(() =>
{
return _mediaFile.GetStream();
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
}
}
public async void OpenMyCamera()
{
try
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
ShowAlert("No camera available.");
return;
}
_mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
AllowCropping = true
});
if (_mediaFile == null)
return;
tweetPicture.Source = ImageSource.FromStream(() =>
{
return _mediaFile.GetStreamWithImageRotatedForExternalStorage();
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
}
}
错误信息:
插件版本号:4.0.1.5
测试设备:红米 Note 5 Pro
VS 版本:15.9.3
Xamarin 版本:3.4.0.1008975
任何人都可以为此提出解决方案吗?
在 manifest/info.plist 文件中设置权限是第一步,但用户还需要授予这些权限。
您始终可以添加权限检查以确保用户确实允许该应用访问 camera/location/photos/whatever。
查看James Montemagno's Permissions Plugin权限检查和请求。