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

Expo iOS端调用MediaLibrary.createAssetAsync保存视频失败问题求助

问题根因

这个问题是Expo SDK 42配套的expo-media-library在iOS 14.8+系统的Expo Go运行环境下的已知兼容性bug:相机录制生成的临时缓存视频文件的沙盒读取权限被系统限制,createAssetAsync方法无法直接读取缓存路径下的mov文件导致保存失败,照片因为格式和缓存逻辑不同不受影响。


修复方案

1. 核心逻辑

先把录制生成的临时视频文件移动到应用的文档目录,再调用createAssetAsync保存移动后的文件路径,即可绕过缓存目录的权限限制,该修改对Android端无影响,不需要额外做平台判断。

2. 具体实现代码

import * as FileSystem from 'expo-file-system';

// 原有录制逻辑保持不变
const recordPromise = this.camera.recordAsync({
    maxDuration: 5,
    quality: Camera.Constants.VideoQuality["720p"],
});
const videoData = await recordPromise;

// 新增:移动缓存文件到应用文档目录
const targetUri = FileSystem.documentDirectory + videoData.uri.split('/').pop();
await FileSystem.moveAsync({
  from: videoData.uri,
  to: targetUri
});

// 用新路径保存资源到相册
let asset = await MediaLibrary.createAssetAsync(targetUri);

// 可选:保存完成后删除文档目录下的临时文件,避免占用存储空间
await FileSystem.deleteAsync(targetUri);

3. 依赖安装

确保项目安装了对应SDK版本的expo-file-system依赖,SDK 42对应兼容版本为~11.1.3,执行以下命令安装即可:

expo install expo-file-system@11.1.3

验证说明

不需要调整现有权限申请逻辑,原有权限配置已经满足需求,修复后在Expo Go环境下直接热重载即可验证,不需要重新打包。

内容的提问来源于stack exchange,提问作者Sebastien F.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 18:15:02