You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic框架iOS端视频URI转Blob失败,指定回调未执行求助

问题排查与修复方案

1. 修正Camera的DestinationType配置

你当前设置的destinationType: this.camera.DestinationType.DATA_URL不适用于视频文件——该类型会将媒体内容转为base64字符串,而非文件路径。后续代码中对videoUrl进行路径截取(提取filename和dirpath)的操作完全无效,直接导致后续文件读取流程出错。

修改CameraOptions:

const options: CameraOptions = {
  sourceType: this.camera.PictureSourceType.PHOTOLIBRARY, 
  destinationType: this.camera.DestinationType.FILE_URI, // 替换为FILE_URI或NATIVE_URI
  mediaType: this.camera.MediaType.VIDEO      
}

2. 处理makeFileIntoBlob的Promise错误

调用makeFileIntoBlob时,你没有处理其可能抛出的错误,导致出错时无法捕获日志,也无法定位问题。修改调用逻辑,添加await和错误捕获:

try {
  var dirUrl = await this.file.resolveDirectoryUrl(dirpath);
  var retrievedFile = await this.file.getFile(dirUrl, filename, {});
  // 新增Promise处理逻辑,捕获转换过程中的错误
  await this.makeFileIntoBlob(retrievedFile.nativeURL)
    .then(result => {
      console.log("Blob转换成功", result.imgBlob);
      // 这里可添加Blob的后续处理逻辑
    })
    .catch(err => {
      console.error("文件转Blob失败", err);
      this.ionLoader.showAlert("文件转Blob失败: " + err.message);
    });
} catch(err) {
  console.error("文件路径解析出错", err);
  return this.ionLoader.showAlert("Something went wrong.");
}

3. 优化makeFileIntoBlob中的冗余代码

在makeFileIntoBlob的第一个then块中,你重复调用了readAsArrayBuffer,且提前执行的var buffer=this.file.readAsArrayBuffer(path, name);返回的是Promise对象而非实际buffer,这部分冗余代码可删除,直接返回Promise即可。同时建议动态匹配视频MIME类型,避免硬编码错误:

makeFileIntoBlob(_imagePath) {
  return new Promise((resolve, reject) => {
    console.log("makefileintoblob", _imagePath);
    let fileName = "";
    this.file
      .resolveLocalFilesystemUrl(_imagePath)
      .then(fileEntry => {
        let { name, nativeURL } = fileEntry;
        let path = nativeURL.substring(0, nativeURL.lastIndexOf("/"));
        console.log("path", path);
        console.log("fileName", name);
        fileName = name;
        // 直接返回readAsArrayBuffer的Promise,无需提前执行
        return this.file.readAsArrayBuffer(path, name);
      })
      .then(buffer => {
        this.ionLoader.showAlert('2:' + buffer);
        console.log("makefileintoblob buffernew: ", buffer);
        // 动态获取视频MIME类型
        let mimeType = this.getVideoMimeType(fileName);
        let imgBlob = new Blob([buffer], { type: mimeType });
        console.log(imgBlob.type, imgBlob.size);
        resolve({ fileName, imgBlob });
      })
      .catch(e => {
        console.error("makeFileIntoBlob流程出错", e);
        reject(e);
      });
  });
}

// 辅助函数:根据文件名后缀匹配对应MIME类型
getVideoMimeType(filename: string): string {
  const ext = filename.split('.').pop()?.toLowerCase();
  switch(ext) {
    case 'mov': return 'video/quicktime';
    case 'mp4': return 'video/mp4';
    case 'avi': return 'video/x-msvideo';
    default: return 'video/quicktime';
  }
}

4. 验证iOS相册权限

确保应用拥有访问相册的权限,检查config.xml中是否添加了权限配置:

<platform name="ios">
  <config-file parent="NSPhotoLibraryUsageDescription" platform="ios" target="*-Info.plist">
    <string>需要访问相册以选择视频文件</string>
  </config-file>
</platform>

内容的提问来源于stack exchange,提问作者Manthiram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 04:55:32