简体   繁体   English

如何在颤动中获取图像的路径?

[英]how to get the path of an image in flutter?

in my app y take a photo and then save it in the device, but then i dont know how to get the path of that image, cause the dir is root.在我的应用程序中,您拍了一张照片,然后将其保存在设备中,但是我不知道如何获取该图像的路径,因为该目录是根目录。 this my code:这是我的代码:

This is where i take the photo:这是我拍照的地方:

  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;

} }

this is the trigger:这是触发器:

  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');
  }
});

} }

and this is where i want to send the image in formdata:这是我想在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);
  });
}

when i take a photo thats returns:当我拍照返回时:

{"error":"http: no such file"} {“错误”:“http:没有这样的文件”}

it's as if the api did not receive anything.就好像 api 没有收到任何东西一样。 i think is because the location is only root.我认为是因为该位置只是根目录。

how can i modify that to access to the image?我如何修改它以访问图像?

I tried the code you've provided retaining only the parts to generate a file path and it works without issues.我尝试了您提供的代码,仅保留部分来生成文件路径,并且它可以正常工作。 With getApplicationDocumentsDirectory() , I assume that you're using path_provider package.使用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());
}

I suggest verifying if the image has been created to troubleshoot the issue.我建议验证是否已创建图像以解决问题。 The error from the logs provided seems to originate from your upload() task.提供的日志中的错误似乎源自您的upload()任务。

Also, DelegatingStream.typed() has been deprecated.此外, DelegatingStream.typed()已被弃用。 Please http.ByteStream(StreamView<List<int>>).cast() instead.http.ByteStream(StreamView<List<int>>).cast()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM