【问题标题】:Ionic - EACCES (Permission denied) error when uploading videoIonic - 上传视频时出现 EACCES(权限被拒绝)错误
【发布时间】:2020-12-28 02:20:20
【问题描述】:

我在尝试使用 Cordova Camera Plugincordova-plugin-advanced-http 将视频文件上传到服务器时遇到了一些困难。从图库中上传 图像 时,该代码的作用就像一个魅力,但无论我做什么,从图库中上传 视频 时我总是会收到 EACCES (Permission denied)

file url ->  file:///storage/emulated/0/DCIM/Camera/VID_20200908_114957.mp4
post-post-module-es2015.js:240 {status: -1, error: "There was an error with the request: /storage/emulated/0/DCIM/Camera/VID_20200908_114957.mp4: open failed: EACCES (Permission denied)"

只看错误信息,我们可以断定这是一个权限问题,所以我尝试使用cordova-plugin-android-permissions 并请求 READ_EXTERNAL_STORAGE 权限。不成功,应用有权限但错误依旧。

这是用于上传的部分代码

private chooseContentUsingCameraPlugin(SOURCE: number) {
    const options: CameraOptions = {
        destinationType: this.camera.DestinationType.FILE_URI,
        mediaType: this.camera.MediaType.ALLMEDIA,
        sourceType: SOURCE
    };
    this.camera.getPicture(options).then((contentUrl: string) => {
        if (contentUrl.indexOf('://') === -1)
            contentUrl = 'file://' + contentUrl;
        const queryIndex = contentUrl.lastIndexOf('?');
        if (queryIndex !== -1)
            contentUrl = contentUrl.substring(0, queryIndex);
        console.log('file url -> ', contentUrl);
        this.startUpload(contentUrl);
    }, (err) => this.onUploadError(err));
}

private startUpload(fileUrl){
    ...
    this.nativeHttp.uploadFile(req.url, null, headers, fileUrl, fileName).then(res => {
        let data = res.data;
        if (res.data && (req.responseType === undefined || req.responseType === 'json'))
            data = JSON.parse(res.data);
        console.log(data)
    }).catch(error => {
        console.log(error)
    });
}

有人可以解释导致此问题的原因吗?

【问题讨论】:

    标签: android cordova ionic-framework cordova-plugins


    【解决方案1】:

    您尝试上传的文件可能不在您的许可范围内。这就是documentation 所说的:

    这是一个软限制权限,在记录的安装程序将该权限列入白名单之前,应用程序不能以其完整形式持有该权限。具体来说,如果权限被列入白名单,则持有人应用程序可以访问外部存储和视觉和听觉媒体集合,而如果权限未列入白名单,则持有人应用程序只能访问视觉和听觉媒体集合。此外,权限是不可变的,这意味着白名单状态只能在安装时指定,并且在安装应用程序之前不能更改。更多详情请见PackageInstaller.SessionParams.setWhitelistedRestrictedPermissions(Set)

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 2017-03-13
      • 2021-07-22
      • 2020-12-17
      • 2019-10-28
      • 2016-10-30
      • 2018-06-08
      • 2018-03-25
      • 1970-01-01
      相关资源
      最近更新 更多