Jonel Dominic Tapang
Jonel Dominic Tapang

Reputation: 496

How to Save ByteData (Image) into Download directory in Flutter?

I'm trying to save a ByteData image generated from canvas into the device download folder. However, I was only able to save it into my apps android/data/ directory using the code below...

Future<void> saveImage(
  String fileName,
    ByteData image,
  ) async {
    final directoryName = "Generated";
    
    Directory directory = await getExternalStorageDirectory();
    String path = directory.path;
    await Directory('$path/$directoryName').create(recursive: true);
    
    File('$path/$directoryName/$fileName.png').writeAsBytesSync(image.buffer.asInt8List());
}

but, it's not convenient for the user to go into this deep directory just to get the image, so I want to save it into the Download directory but I don't know how. I have tried several approaches but I keep on receiving permission errors. Like for example using this code below (downloads_path_provider)...

Directory directory = await DownloadsPathProvider.downloadsDirectory;
String path = directory.path;  

File('$path/$fileName.png').writeAsBytesSync(image.buffer.asInt8List());

PS. It would be much better if there's a way to save it somewhere that will show in the Phone's Gallery

Upvotes: 1

Views: 1811

Answers (1)

bluenile
bluenile

Reputation: 6029

You can save images to gallery with this package on pub.dev : 'gallery_saver' : https://pub.dev/packages/gallery_saver

Upvotes: 1

Related Questions