我需要在哪里将照片图像存储在Flutter App中?

时间:2019-10-03 20:17:47

标签: flutter dart

我有一个可以拍照的应用程序。

我需要将该照片存储在我所有资产/图像/照片所在的位置。

所以基本上我的问题是:  a)如何找到这些资产(在电话上)的路径?  b)如何将文件添加到同一路径?

谢谢

1 个答案:

答案 0 :(得分:3)

据我所知,您无法将图片存储在资产包中。仅仅是因为当我们在资产文件夹为只读之后构建android / ios应用程序时。

您需要将图像存储在本地移动存储或云或捕获内存中

1)您可以使用:

获取存储路径。
  Future<String> getStorageDirectory() async {
    if (Platform.isAndroid) {
      return (await getExternalStorageDirectory()).path;  // OR return "/storage/emulated/0/Download";
    } else {
      return (await getApplicationDocumentsDirectory()).path;
    }
  }

2)在路径中添加图片

createImage() async{
  String dir= getStorageDirectory();

  File directory = new File("$dir");
  if (directory.exists() != true) {
    directory.create();
  }
  File file = new File('$directory/image.jpeg');
  var newFile = await file.writeAsBytes(/* image bytes*/);
  await newFile.create();
}