【问题标题】:How to save image in Image widget to android device?如何将图像小部件中的图像保存到安卓设备?
【发布时间】:2020-01-18 05:55:22
【问题描述】:

我有一个包含图像的图像小部件,我想将该图像保存到 Android 设备以供以后使用。

我找到了这个topic 但我找不到将图像转换为文件的方法。

所以任何人都知道如何在 Flutter 中将 Image 转换为 File 或直接将 Image 小部件中的图像保存到 Android 设备。

谢谢。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    试试看image_picker_saver

    或者

    您需要将图像保存到外部存储目录中才能在图库中显示图像。不获取临时目录,而是获取外部存储目录。

    final directory = await getExternalStorageDirectory();
    

    你需要提供你的android/app/src/main文件夹的AndroidManifest.xml文件的权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    然后假设您要创建一个名为 MyImages 的文件夹并将新图像添加到该文件夹​​中,

    final myImagePath = '${directory.path}/MyImages' ;
    final myImgDir = await new Directory(myImagePath).create();
    

    然后将文件写入路径。

    var kompresimg = new File("$myImagePath/image_$baru$rand.jpg")
      ..writeAsBytesSync(img.encodeJpg(gambarKecilx, quality: 95));
    

    要获取文件的数量,只需将文件获取到一个列表并检查列表的长度

    var listOfFiles = await myImgDir.list(recursive: true).toList();
    var count = countList.length;
    

    【讨论】:

    • 感谢您的详细回答。我试试看
    • 好像我没有访问该文件的权限。它显示此错误:创建失败,路径 = '/storage/emulated/0/MyImages'(操作系统错误:权限被拒绝,errno = 13)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2020-11-18
    • 1970-01-01
    • 2020-06-22
    • 2012-06-27
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多