简体   繁体   中英

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

您可以在 pub.dev 上使用此包将图像保存到画廊:'gallery_saver': https ://pub.dev/packages/gallery_saver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM