首页 > 解决方案 > 如何在颤动中获取图像的路径?

问题描述

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

这是我拍照的地方:

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

当我拍照时返回:

{“错误”:“http:没有这样的文件”}

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

我如何修改它以访问图像?

标签: androidiosdartflutterform-data

解决方案


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

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<List<int>>).cast()改为。

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

推荐阅读