Flutter: How to get a specific album with photo_manager?

1.9k views Asked by At

I'm working on an application where the user is only really working from a single album. I don't want to display all of the users pictures while they are in the app, just the ones that they will be dealing with. I'm using photo_manager and it's great for getting all of the images, but I can't find a way to get just one album - all of the tutorials I've found for working with photo_manager just pull all the images from the phone. filteroptionsgroup seemed promising but doesn't provide a way to identify an album via string. I can tell AssetEntity gets the name of albums but I don't know how to compare it. Here's the code so far:

   class gallery extends StatefulWidget {
      gallery({Key? key}) : super(key: key);

       @override
     State<gallery> createState() => _galleryState();
    }

    class _galleryState extends State<gallery> {
      List<AssetEntity> assets = [];
      _fetchAssests() async {
final FilterOptionGroup filter = new FilterOptionGroup();
final albums = await PhotoManager.getAssetPathList(
    type: RequestType.image, filterOption: filter);
// this is where I am lost, I don't know how to get a specific album.

final wordpicsalbum = albums.first;
final recentAssets = await wordpicsalbum.getAssetListRange(
  start: 0,
  end: 50000,
);

setState(() {
  assets = recentAssets;
});
}

     @override
    void initState() {
      _fetchAssests();
      super.initState();
    }

Formatting is weird between my setup in vscode and what SO expects, sorry about that.

3

There are 3 answers

3
Piotr On

Have you tried using AssetPathEntity instead of AssetEntity?

Get albums/folders (AssetPathEntity) Albums or folders are abstracted as the AssetPathEntity class. It represent a bucket in the MediaStore on Android, and the PHAssetCollection object on iOS/macOS. To get all of them:

final List<AssetPathEntity> paths = await PhotoManager.getAssetPathList();

See getAssetPathList for more detail.

0
Wleeches On

I used the "relativePath" attribute of AssetEntity and used a for loop to look for images that were saved under the album name:

 List<AssetEntity> galery = [];
    for (var i in recentAssets) {
      if 
(i.relativePath.toString().contains("albumname")) {
        gallery.add(i);
      }
    }
0
Marcus On

You can use below to target specific album name.

final List<AssetPathEntity> paths = await PhotoManager.getAssetPathList();
AssetPathEntity? path = paths.where((path) => path.name == "YourAlbumName").first;