【问题标题】:How to Save ByteData (Image) into Download directory in Flutter?如何将字节数据(图像)保存到 Flutter 的下载目录中?
【发布时间】:2020-11-01 05:53:14
【问题描述】:

我正在尝试将从画布生成的 ByteData 图像保存到设备 download 文件夹中。但是,我只能使用下面的代码将其保存到我的应用程序 android/data/ 目录中......

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());
}

但是,用户进入这个深层目录只是为了获取图像并不方便,所以我想将它保存到下载目录但我不知道如何。我尝试了几种方法,但我不断收到权限错误。例如使用下面的代码 (downloads_path_provider)...

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

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

PS。如果有办法将它保存在手机图库中会显示的某个地方会更好

【问题讨论】:

    标签: flutter


    【解决方案1】:

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

    【讨论】:

    • 酷!谢啦。我所做的只是首先将 ByteData 图像保存到一个临时目录中......然后将路径与文件名一起保存到画廊中使用 GallerySaver
    猜你喜欢
    • 1970-01-01
    • 2012-08-13
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多