如何在Flutter中获取图片路径?

3
在我的应用中,我拍了一张照片并将其保存在设备上,但是我不知道如何获取该图像的路径,因为该目录是根目录。这是我的代码:
这是我拍照的地方:
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);
    });
}

当我拍照时,返回以下内容:

{"error":"http: no such file"}

似乎API没有收到任何信息。我认为这是因为位置仅为根目录。
我该如何修改以访问图像?

我正在努力找出哪个部分出错了。我现在无法运行你的代码,你能提供一个最小、完整且可验证的示例吗?这样我就能追踪你的实现过程中发生了什么。另外,我注意到你正在尝试捕获图像并将其保存到设备上。据我所知,有一个现有的插件可以实现这个功能,即image_picker - undefined
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接