如何获得抖动图像的路径?

时间:2018-07-05 17:52:34

标签: android ios dart flutter form-data

在我的应用程序中

y拍张照片,然后将其保存在设备中,但随后我不知道如何获取该图像的路径,因为该目录是root。这是我的代码:

这是我拍照的地方:

  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没有收到任何东西。 我认为是因为该位置仅是根。

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

1 个答案:

答案 0 :(得分:0)

我尝试了您提供的代码,只保留了生成文件路径的部分,并且可以正常工作。对于 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<List<int>>).cast()

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