【发布时间】: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