如何在 flutter 中从图库中检索图像

Jun*_*ned 2 dart flutter

我正在尝试在 Flutter 中制作一个图像选择器,我可以从图库中选择图像。

我不知道图像选择器的一些内置代码或库。

Tah*_*las 5

我知道我可能有点晚了,但你可以使用这个库。

它返回图库以及当前正在开发的图库的图像,但应该可以在 Android 上完美运行,而对于 iOS 来说,它尚未完全实现。

如何使用它

获取专辑:
List<PhoneAlbum> phoneAlbums = [];
final customImagePicker = CustomImagePicker();


Future<void> getGallery() async {
    try {
      await customImagePicker.getAlbums(callback: (msg) {
        setState(() {
          phoneAlbums = msg;
        });
      });
    } on PlatformException {}
  }
Run Code Online (Sandbox Code Playgroud)

手机相册有这些属性

String id;
String name;
String coverUri;
int photosCount;
Run Code Online (Sandbox Code Playgroud) 要获取相册的照片:
 List<PhonePhoto> images = [];

 final customImagePicker = CustomImagePicker();

Future<void> getPhotosOfGallery(String albumID) async {
    List<PhonePhoto> allImages = [];
    try {
      final cancelElement = await customImagePicker.getPhotosOfAlbum(albumID, callback: (msg) {
        print('The message is $msg');
      });
    } on PlatformException {}

    setState(() {
      images = allImages;
    });
  }
Run Code Online (Sandbox Code Playgroud)

手机照片具有以下属性:

 String id;
 String albumName;
 String photoUri;
Run Code Online (Sandbox Code Playgroud)

这可以帮助您以您喜欢的方式显示图像,并且很快就可以获取视频,以及分页(延迟加载)以提高性能

https://pub.dev/packages/custom_image_picker