Flutter图像拾取器明确请求android版本33及更高版本的权限

y4ekin9u  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(303)

Android 33及更高版本图库无法打开,且未请求权限。
permission.storage.request()在android13上允许时返回permanentlyDenied

0h4hbjxa

0h4hbjxa1#

如果您的应用面向Android 13或更高版本,并且需要访问其他应用创建的媒体文件,则您必须请求以下一项或多项粒度媒体权限,而不是READ_EXTERNAL_STORAGE权限:
https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions

机器人清单:

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

本发布仅限照片和视频

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>

公共规范名称:

device_info_plus: ^8.0.0

清除并获取依赖项以避免应用程序崩溃

flutter clean && flutter pub get

无论您的代码在何处:

bool isStoragePermission = true;
bool isVideosPermission = true;
bool isPhotosPermission = true;

// Only check for storage < Android 13
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
if (androidInfo.version.sdkInt >= 33) {
  isVideosPermission = await Permission.videos.status.isGranted;
  isPhotosPermission = await Permission.photos.status.isGranted;
} else {
  isStoragePermission = await Permission.storage.status.isGranted;
}

if (isStoragePermission && isVideosPermission && isPhotosPermission) {
  // no worries about crash 
} else {
  // write your code here
}

相关问题