uni-app 视频上传API 接口调用失败

uni-app 视频上传API 接口调用失败

代码

uni.chooseVideo({  
success: res => {  
this.videoUrl = res.tempFilePath;  
},  
fail: (res) => {  
console.log(res);  
}  
})  

错误信息

{  
"errMsg": "chooseVideo:fail Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference",  
"errCode": 13,  
"code": 13  
}  

在选择文件夹的视频后接口的调用失败,部分视频会出现这样的问题,有的视频能正常调用api


更多关于uni-app 视频上传API 接口调用失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 视频上传API 接口调用失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个典型的Android平台兼容性问题。错误信息表明在调用chooseVideo时出现了空指针异常,通常由以下原因导致:

  1. 视频元数据读取失败:部分视频文件的元数据(如编码格式、时长等)无法被系统正确解析,导致获取到的tempFilePath为null。

  2. 系统媒体库异常:Android系统媒体库在处理某些特殊编码或损坏的视频文件时可能出现问题。

  3. 文件路径权限问题:某些设备上临时文件路径访问权限受限。

解决方案:

  1. 添加文件类型过滤
uni.chooseVideo({
  sourceType: ['album'],
  compressed: false,
  maxDuration: 60,
  success: res => {
    if(res.tempFilePath){
      this.videoUrl = res.tempFilePath;
    }
  },
  fail: (err) => {
    console.error('选择视频失败:', err);
  }
})
  1. 检查文件有效性: 在选择视频后,先验证文件路径是否存在:
success: res => {
  if(res.tempFilePath){
    uni.getFileInfo({
      filePath: res.tempFilePath,
      success: (fileInfo) => {
        this.videoUrl = res.tempFilePath;
      },
      fail: () => {
        uni.showToast({title: '视频文件无效', icon: 'none'});
      }
    })
  }
}
回到顶部