0

I'm new in flutter. Hope my issue below will be same your issue in the past. And you can help me for this:

My situation is: My app will capture an photo (using package camera: ^0.5.8+2). Then, this photo will be saved on gallery of user device (using package gallery_saver: ^2.0.1). Finally, I will open the gallery (using package photo_manager: ^0.5.7) to pick some photos which I captured before ==> This flow is really clear and simple, right? ^_^

But, I have already trap at this issue: After I captured an photo, then open the gallery from my app, I can't see this photo (Althought I can see this photo on device gallery). I also print the log on console to make sure that: my recently photo was got by my app. But, I still see nothing, it's mean my app also can't read this recently photo.

So, what do I wrong? Or what am I missing? Everyone who have experience about this issue help me, pls!

Below is my source code:

This is function to take photo by using camera package (https://pub.dev/packages/camera)

/// Take an photo
  _takePhoto() async {
    try {
      if (_numberPhotoTaken < widget.maxTaken) {
        /// Path
        var file = "${DateTime.now().toString()}.jpg";
        
        var temporaryDir = (await getTemporaryDirectory()).path;

        var pathSaved = join(temporaryDir, file);

        /// Take photo
        await _cameraController.takePicture(pathSaved);

        /// save to gallery
        GallerySaver.saveImage(pathSaved, albumName: galleryFolder).then((ok) {

          if (ok) {
            setState(() => _numberPhotoTaken++);
          }

          /// remove temporary file
          File(pathSaved).delete();
        });
      } else {
        AppDialog.inform(
            this.context,
            textLocale("Reached the maximum number of photos", this.context),
            () {});
      }

    } on CameraException catch (e) {
      
    } on Exception catch (ex) {
      
    }
  }

This is function to load asset from gallery by using photo manager package (https://pub.dev/packages/photo_manager)

/// fetching gallery's asset
  /// return: list of asset entity in specific page
  Future<List<GalleryEntity>> _fetchGalleryPaging() async {
    /// load all assets in one album list
    List<AssetPathEntity> albums = await PhotoManager.getAssetPathList(
        onlyAll: true, type: RequestType.common);

    List<AssetEntity> page = await albums[0].getAssetListPaged(_currentPage, widget.perPage);

    /// return gallery entity list
    return page.map<GalleryEntity>((e) => GalleryEntity(asset: e)).toList();
  }

I'm currently test on my android device

When I open gallery on device, I wonder: the new photo which I have just captured not be arranged right (the newest photo must be at the end of gallery). After a few time (I'm not sure how many time), device gallery will auto refresh, and my recently photo will be arranged in the right way (it's at the end of gallery). And my app will read this photo perfectly (so strange for me)

UPDATE: one more thing, my photo will be saved into a separated folder in gallery

If you read till this line. I'm so appreciate about that. And I hope among of you have experience to solve this issue

Thanks all guys,

Alex
  • 11
  • 2

1 Answers1

0

After a few time I made searching about my issue. I find out the main problem is the external storage was not rescan when I did an capturing photo, so my flutter app can not be display the new photo on gallery.

The solution for this issue is the package:

https://github.com/hui-z/image_gallery_saver

I'm not sure about the effective of this package, but for my scene, it's work as perfect as I wish.

Hope this help for anyone who also face same issue like me Thanks all

Alex
  • 11
  • 2