首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将imagePath更改为文件?

问如何将imagePath更改为文件?
EN

Stack Overflow用户
提问于 2020-09-11 09:11:12
回答 1查看 158关注 0票数 0

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

这是拍摄照片的函数

代码语言:javascript
复制
_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:“传递。

代码语言:javascript
复制
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)),
      ),
    );
  }

这是我需要帮助的save函数。我需要得到的imagePath是一个文件,以便这一部分可以工作,我可以添加逻辑,以保存照片到火库。

代码语言:javascript
复制
controlUploadAndSave() async {
    setState(() {
      uploading = true;
    });

    await compressingPhoto();

    String downloadUrl = await uploadPhoto(file);

    savePostInfoToFireStore(url: downloadUrl);

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

回答 1

Stack Overflow用户

发布于 2020-09-11 11:21:08

您实际上已经做到了这一点。使用File(path)返回给定路径中的文件。

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

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

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

主要问题是您使用的是函数而不是小部件。对于新屏幕(ConfirmationScreen),您应该有一个单独的小部件(如果需要,可以有单独的状态)。然后,您可以使用类似以下内容:

代码语言:javascript
复制
MaterialPageRoute(
  builder: (context) => ConfirmationScreen(imageFile: File(imagePath)),
),

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63839507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档