flutter 在Android13上集成permission_handler包以访问手机存储

cpjpxq1n  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(954)

集成的包permission_handler请求权限访问手机的存储后,代码与Android 9及以下,但与Android 13它不工作在所有,我真的卡住:这是我的清单代码:

<uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.WAKE_LOCK"/>
   <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
   <uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.FLASHLIGHT" />
   <uses-feature android:name="android.hardware.camera" />
   <uses-feature android:name="android.hardware.camera.flash" />
   <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="32" />

下面是main的代码:

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();
const AndroidInitializationSettings initializationSettingsAndroid =
    AndroidInitializationSettings('ic_notification');

const InitializationSettings initializationSettings = InitializationSettings(
  android: initializationSettingsAndroid,
);

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onDidReceiveNotificationResponse: (NotificationResponse response) {});

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]).then((value) => runApp(const App()));

  await JustAudioBackground.init(
    androidNotificationChannelId: 'com.ryanheise.bg_demo.channel.audio',
    androidNotificationChannelName: 'Audio playback',
    androidNotificationOngoing: true,
  );

  await AccountServices().initialization.then((value) {
    Get.put(CartController());
  });
}

下面是代码执行后的错误返回:

E/flutter (  897): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7)
E/flutter (  897): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:315:18)
E/flutter (  897): <asynchronous suspension>
E/flutter (  897): #2      AndroidFlutterLocalNotificationsPlugin.initialize (package:flutter_local_notifications/src/platform_flutter_local_notifications.dart:151:12)
E/flutter (  897): <asynchronous suspension>
E/flutter (  897): #3      main (package:rmfitness/main.dart:34:3)
E/flutter (  897): <asynchronous suspension>
oxiaedzo

oxiaedzo1#

从Android 13开始,无需询问READ_EXTERNAL_STORAGE权限。您已根据需要将其替换为READ_MEDIA_IMAGES和READ_MEDIA_VIDEO。(这是您的应用针对SDK 33时)
例如:
1.图片和照片:READ_MEDIA_IMAGES,
1.视频:READ_MEDIA_VIDEO音频,
1.音频文件:阅读_媒体_音频
参考此android 13 behavior changes
现在,对于您的错误,您必须为Android 13添加通知运行时权限。

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

引用此link

相关问题