flutter getTemporaryDirectory()的使用

下面是上传音视频流的截图,先保存在缓存,然后请求接口,成功或者失败会删除文件。
可以在Device File Explorer查看,
Android:

会返回 /data/data//cache 这个目录,是应用私有的缓存目录。

iOS:

会返回 Library/Caches 下的一个目录,是应用共享的缓存目录。

  //上传音视频截图
  postSnapshotRequest(
      {required int sendUserId,
      required int recUserId,
      required String order,
      required Uint8List imageBytes}) async {
    final tempDir = await getTemporaryDirectory();
    var file = await File(
            '${tempDir.path}/image_${DateTime.now().millisecondsSinceEpoch}.jpg')
        .create();

    file.writeAsBytesSync(imageBytes);

    PrintUtil.prints('图片裁剪 文件地址:${file.path}');

    String path = file.path;
    String name = path.substring(path.lastIndexOf('/') + 1, path.length);
    PrintUtil.prints("tag_图片路径path $path");
    PrintUtil.prints("tag_图片路径name $name");
    var attFile = await MultipartFile.fromFile(path, filename: name);
    FormData formData = FormData.fromMap({
      'capture_img': attFile,
      "order_no": order,
      "c_user_id": sendUserId,
      "r_user_id": recUserId
    });

    httpReq.HttpRequest.intance
        .postHttpFromBusiness(Api.AUDIO_AND_VIDEO_CAPTURE, (data) {
      deleteFile(path);
    }, (error, errorCode) {
      deleteFile(path);
    }, parameters: formData);
  }
  
  void deleteFile(String filepath) {
    var file = File(filepath);
    if (file.existsSync()) {
      file.deleteSync();
    }
  }

你可能感兴趣的:(flutter,flutter)