natsuk4ze / gal

How to Save Image or Video to Photo Gallery in Flutter

Home Page:https://pub.dev/packages/gal

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider add some functions to save incoming bytes stream directly to gallery

ngotandatnnst opened this issue · comments

Proposal

As we discuss at #128, sometime we need to push directly incoming bytes stream to a file in gallery

For example:

// Download A Large Video Using Dio
File videoFile = Gal.getVideo('video.mp4',  album: '$album', createIfNotExist: true);
await Dio().download('$url', videoFile.path)

// Or Manual Controll Stream
File videoFile = Gal.getVideo('video.mp4',  album: '$album',  createIfNotExist: true);
IOSink videoSink = videoFile.openWrite();

response.stream.listen(
  (List<int> newBytes) async {
    videoSink.add(newBytes);
  },
  onDone: () async {
    await videoSink.flush();
  },
  onError: () {
    unawaited(videoSink.close().then((value) => videoFile.delete()));
  },
  cancelOnError: true,
);

Use case

If the file is large, it take more phone's resource to

download file to temp directory -> copy to gallery by using Gal.putVideo -> delete file in temp directory

Resource for proccessing that flow is too expensive if the downloading file is large. So, we need somehow to push incoming bytes stream to a file in gallery. And then the flow is just simple like

download file to gallery

In addition, if donwloading file isn't too large, that feature still make our code shorter and clearer. So, our code are not errorprone

Thank you for submitting the issue ! @ngotandatnnst 🤝.

I have researched the native APIs swift photo-kit and android MediaStore and could not find a way to implement this feature.
For photo-kit, We need to specify the local URL of the file,

: PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)!

For MediaStore, saving directly to the "Pictures" or "Movies" directory, which is the destination for gallery, has been deprecated since Android 10.

Are there other packages that implement this feature?

Apart from that, there is a good package called ffmpeg_kit_flutter as a general optimization approach for large videos. It allows you to convert videos to higher compression video codecs and file formats, split videos, and more.

I hope this helps 💚

I don't know that is not allowed by native APIs. Thanks for spending time and for usefull infomation.

In fact, I am finding a package allow me to do that feature but hasn't found yet (now I know it's not allowed by native APIs 😅 ). I just changed using packages like image_gallery_saver and saver_gallery to your Gal package because your Gal packge is clearer, up to date, and your support is extremely greate base on reply time and useful infomation

Hope you keep going this awesome package and create more usefull packages

I am sorry @ngotandatnnst. But your message helps me to maintain this package ! Thank you 💚