【问题标题】:How can I change an imagePath to a file?如何将 imagePath 更改为文件?
【发布时间】:2020-09-11 01:11:11
【问题描述】:

我拍了一张照片,它被存储在一个临时目录中,因此可以在发布之前在预览屏幕上查看。我正在尝试弄清楚如何将此 imagePath 转换为文件,以便将其存储在 Firebase 中。

这是拍照功能

_onCapturePressed(context) async {
    // Take the Picture in a try / catch block. If anything goes wrong,
    // catch the error.
    try {
      // Attempt to take a picture and log where it's been saved
      final path = join(
        // In this example, store the picture in the temp directory. Find
        // the temp directory using the `path_provider` plugin.
        (await getTemporaryDirectory()).path,
        '${DateTime.now()}.png',
      );
      //print(path);
      await controller.takePicture(path);
      imagePath = path;

      // If the picture was taken, display it on a new screen
      Navigator.push(
        this.context,
        MaterialPageRoute(
          builder: (context) => displayStoryUploadScreen(),
        ),
      );
    } catch (e) {
      // If an error occurs, log the error to the console.
      print(e);
    }
  }

这是预览图片的displayUploadScreen函数。

imageFile 是通过“body:”传递的

displayStoryUploadScreen() {
    print(imagePath);
    //Navigator.pop(context);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.blue),
        title: Text(
          "Preview",
          style: TextStyle(
            color: Colors.blue,
            fontSize: 22.0,
          ),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.done,
              color: Colors.blue,
              size: 30.0,
            ),
            onPressed: () => controlUploadAndSave(),
          ),
        ],
      ),
      // The image is stored as a file on the device. Use the `Image.file`
      // constructor with the given path to display the image.
      body: Image.file(File(imagePath),
        //decoration: BoxDecoration(image: DecorationImage(image: FileImage(file), fit: BoxFit.cover)),
      ),
    );
  }

这是我需要帮助的保存功能。我需要让 imagePath 成为一个文件,这样这部分才能工作,我可以添加将照片保存到 Firebase 的逻辑。

controlUploadAndSave() async {
    setState(() {
      uploading = true;
    });

    await compressingPhoto();

    String downloadUrl = await uploadPhoto(file);

    savePostInfoToFireStore(url: downloadUrl);

    setState(() {
      file = null;
      uploading = false;
      storyPostId = Uuid().v4();
    });
  }

【问题讨论】:

    标签: file flutter path filepath temporary-files


    【解决方案1】:

    您实际上已经这样做了。使用 File(path) 从给定路径返回一个文件。

    编辑:基于额外信息的更多细节:

    您确实应该使用更多参数,而不是您所在州的值,但这最终取决于您。基本上你需要做的就是将你的imagePath 作为参数(或者File(imagePath),如果你不想在函数中转换它)传递给controlUploadAndSave

    另外,您提到您的imagePath 为空存在问题。当您将函数传递给页面路由时,我实际上不知道 Flutter 是否有效。这不正常,您应该创建小部件而不是返回小部件的函数。但是,您也可以在此处将 imagePath 作为参数传递。

    主要问题是您使用函数而不是小部件。您应该为您的新屏幕 (ConfirmationScreen) 提供一个单独的小部件(如果需要,可以使用单独的状态)。然后你可以使用类似的东西:

    MaterialPageRoute(
      builder: (context) => ConfirmationScreen(imageFile: File(imagePath)),
    ),
    

    将您的图像发送到此小部件。完成该屏幕并想要返回上一个屏幕后,您可以简单地弹出导航器并选择包含一个值。该值作为未来返回到您调用 Navigator.push 的位置。

    【讨论】:

    • 好的,那么我在 controlUploadAndSave 函数中做错了什么?打印时文件为空。我想弄清楚如何通过@David
    • 如何将文件传递给controlUploadAndSave 函数?您是否在您的州使用变量?此外,在body 中,您只是在创建一个图像小部件。你从来没有真正将这个 File 保存在任何地方
    • 这就是我需要帮助的地方。我不确定如何在预览屏幕上保存此文件并将文件传递给 controlUploadAndSave 函数。但是我只创建了图像小部件来查看它(以防万一您想返回并重新拍摄照片)。除非按下保存,否则我不希望它被保存。也许我在考虑逻辑错误,这是不可能的。
    • 用更多细节更新了我的答案
    猜你喜欢
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多