android - 如何在颤动中获取图像的路径?
问题描述
在我的应用程序中,您拍了一张照片,然后将其保存在设备中,但是我不知道如何获取该图像的路径,因为该目录是根目录。这是我的代码:
这是我拍照的地方:
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 没有收到任何东西一样。我认为是因为该位置只是根目录。
我如何修改它以访问图像?
解决方案
我尝试了您提供的代码,仅保留生成文件路径的部分,它可以正常工作。使用getApplicationDocumentsDirectory()
,我假设您正在使用path_provider
package.
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();
推荐阅读
- flutter - 如何让 Flutter 应用对网络变化做出反应?
- python - 如何在服务器套接字上发送和接收消息?
- node.js - 参考错误:未读取视图文件夹中的 .ejs 文件
- scikit-learn - scikit-learn 示例中的决策函数
- reactjs - Redux 使用复选框处理状态
- c++ - 试验算法:这个算法有逆向吗?
- ansible - Ansible Tower 3.5 和自定义 python 模块
- mongodb - 如何从 graphql 查询中获取数组或列表?
- r - 条件面板中的字段需要放在侧面板旁边
- javascript - 如果表格单元格的文本与变量相同,则更改其 CSS