Flutter如何编程打开安卓系统图库/iOS照片应用而非选取图片
Flutter跳转系统相册(仅打开不选图)实现方案
iOS端实现说明
你目前使用的photos-redirect:// URL Scheme是社区通用的稳定实现,苹果官方没有公开相册调用的专属Scheme,该方案不存在合规问题,不会导致App Store审核被拒,可正常使用。
安卓端实现步骤
安卓端没有通用的URL Scheme可以直接跳转相册,需要通过调用系统意图实现,推荐使用官方维护的android_intent_plus包完成跳转:
- 先在
pubspec.yaml中添加依赖:
dependencies: flutter: sdk: flutter url_launcher: ^最新稳定版本 android_intent_plus: ^最新稳定版本
- 替换原有逻辑,完整可运行代码如下:
import 'dart:io'; import 'package:url_launcher/url_launcher.dart'; import 'package:android_intent_plus/android_intent.dart'; Future<void> openSystemGallery() async { if (Platform.isIOS) { final Uri galleryUrl = Uri.parse("photos-redirect://"); if (await canLaunchUrl(galleryUrl)) { await launchUrl(galleryUrl); } } else if (Platform.isAndroid) { const AndroidIntent galleryIntent = AndroidIntent( action: 'android.intent.action.VIEW', type: 'image/*', flags: [Flag.FLAG_ACTIVITY_NEW_TASK], ); await galleryIntent.launch(); } }
注意事项
- 上述安卓代码会调用系统默认图库应用打开图片浏览页面,不会触发选图逻辑,符合需求
- 如果需要打开包含视频的相册总页面,可将安卓Intent的type参数修改为
vnd.android.cursor.dir/* - 少数定制ROM如果修改了系统意图的匹配规则,可补充异常捕获逻辑,给用户弹出手动打开相册的提示
- 示例代码使用了url_launcher新版推荐的
canLaunchUrl和launchUrl方法,替换了已废弃的旧API,可避免编译警告
内容的提问来源于stack exchange,提问作者Banghua Zhao
相关产品推荐
相关产品推荐

