【问题标题】:how to get the path of an image in flutter?如何在颤动中获取图像的路径?
【发布时间】:2018-12-14 06:58:55
【问题描述】:

在我的应用程序中,您拍了一张照片,然后将其保存在设备中,但是我不知道如何获取该图像的路径,因为目录是根目录。这是我的代码:

这是我拍照的地方:

  Future<String> takePicture() async {
if (!controller.value.isInitialized) {
  showInSnackBar('Error: select a camera first.');
  return null;
}
final Directory extDir = await getApplicationDocumentsDirectory();
final String dirPath = '${extDir.path}/Pictures/flutter_test';
await new Directory(dirPath).create(recursive: true);
final String filePath = '$dirPath/${timestamp()}.jpg';

if (controller.value.isTakingPicture) {
  // A capture is already pending, do nothing.
  return null;
}

try {
  await controller.takePicture(filePath);
} on CameraException catch (e) {
  _showCameraException(e);
  return null;
}
return filePath;

}

这是触发器:

  void onTakePictureButtonPressed() {
takePicture().then((String filePath) {
  if (mounted) {
    setState(() {
      imagePath = filePath;
      videoController?.dispose();
      videoController = null;
    });


    upload(File(filePath));

    print(filePath);
    if (filePath != null) showInSnackBar('Picture saved to $filePath');
  }
});

}

这就是我想用 formdata 发送图像的地方:

    upload(File imageFile) async {
  // open a bytestream
  var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
  // get file length
  var length = await imageFile.length();

  // string to uri
  var uri = Uri.parse("http://ip/something/else/here");

  // create multipart request
  var request = new http.MultipartRequest("POST", uri);

  // multipart that takes file
  var multipartFile = new http.MultipartFile('file', stream, length,
      filename: basename(imageFile.path));

  // add file to multipart
  request.files.add(multipartFile);

  // send
  var response = await request.send();
  print(response.statusCode);

  // listen for response
  response.stream.transform(utf8.decoder).listen((value) {
    print(value);
  });
}

当我拍照时返回:

{"error":"http: 没有这样的文件"}

就好像 api 没有收到任何东西一样。 我认为是因为该位置只是根目录。

如何修改它以访问图像?

【问题讨论】:

  • 我试图找出哪个部分有错误。我无法运行您的代码 atm,您能否提供 a minimal, complete and verifiable example 以便我能够跟踪您的实施情况。另外,我注意到您正在尝试捕获图像并将其保存到您的设备中。 AFAIK,有一个现有的插件可以实现这个功能,image_picker

标签: android ios dart flutter form-data


【解决方案1】:

我尝试了您提供的代码,仅保留生成文件路径的部分,它可以正常工作。对于getApplicationDocumentsDirectory(),我假设您使用的是path_provider 包。

Future<String> takePicture() async {
  // if (!controller.value.isInitialized) {
  //   showInSnackBar('Error: select a camera first.');
  //   return null;
  // }
  final Directory extDir = await getApplicationDocumentsDirectory();
  final String dirPath = '${extDir.path}/Pictures/flutter_test';
  await new Directory(dirPath).create(recursive: true);
  final String filePath = '$dirPath/${timestamp()}.jpg';

  // if (controller.value.isTakingPicture) {
  //   // A capture is already pending, do nothing.
  //   return null;
  // }

  // try {
  //   await controller.takePicture(filePath);
  // } on CameraException catch (e) {
  //   _showCameraException(e);
  //   return null;
  // }
  return filePath;
}

String timestamp() {
  // DateFormat uses intl package
  // https://pub.dev/packages/intl
  return DateFormat('y-MM-dd-HHms').format(DateTime.now());
}

我建议验证是否已创建映像以解决问题。提供的日志中的错误似乎源自您的 upload() 任务。

另外,DelegatingStream.typed() 已被弃用。请改用http.ByteStream(StreamView&lt;List&lt;int&gt;&gt;).cast()

Stream<List<int>> stream = http.ByteStream(imageFile.openRead()).cast();

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多