在我的应用中,我拍了一张照片并将其保存在设备上,但是我不知道如何获取该图像的路径,因为该目录是根目录。这是我的代码:
这是我拍照的地方:
当我拍照时,返回以下内容:
我该如何修改以访问图像?
这是我拍照的地方:
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 a 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 a response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
当我拍照时,返回以下内容:
似乎API没有收到任何信息。我认为这是因为位置仅为根目录。{"error":"http: no such file"}
我该如何修改以访问图像?