How to Get Access Permission of Media Files with Flutter on iOS

Paul Xiong
Mar 18, 2024

--

Encountered issues multiple times while attempting to programmatically access media files on the iOS simulator. Here are some notes to aid in troubleshooting and resolving these persistent challenges.

Add photo_manager: ^3.0.0-dev.2


Project Name: 'image_classification_mobilenet'
File Path: 'pubspec.yaml'


dependencies:
photo_manager: ^3.0.0-dev.2

Call stack

Call Stack 
ImageSimilarHelper.getImagePaths (/lib/helper/image_similar_helper.dart:96)
ImageSimilarHelper.initHelper (/lib/helper/image_similar_helper.dart:90)
_GalleryScreenState.initState (/lib/ui/gallery.dart:30)

Code PhotoManager.requestPermissionExtend()


Future<List<String>> getImagePaths() async {
List<AssetEntity> images = [];

var result = await PhotoManager.requestPermissionExtend();
if (result.isAuth) {
// Fetch assets from the camera roll
List<AssetPathEntity> assetPathList =
await PhotoManager.getAssetPathList(type: RequestType.image);
if (assetPathList.isNotEmpty) {
// Fetch images from the first camera roll path
images = await assetPathList[0].getAssetListRange(start: 0, end: 100);
}

Note that the code for PhotoManager.requestPermissionExtend() must be in the same isolate as the caller. Otherwise, an error will occur.

--

--

Paul Xiong
Paul Xiong

Written by Paul Xiong

Predicting the next word (token) is what powers ChatGPT, while predicting the next photo (embedding) forms the foundation of ImageGPT.

No responses yet