Ionic-上传视频时出现EACCES(权限被拒绝)错误

时间:2020-09-09 15:50:48

标签: android cordova ionic-framework cordova-plugins

尝试使用Cordova Camera Plugincordova-plugin-advanced-http将视频文件上传到服务器时遇到一些困难。从库上传图像时,代码的工作原理很吸引人,但是无论我做什么,从库上传视频时我总是收到EACCES (Permission denied):< / p>

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)
    });
}

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

1 个答案:

答案 0 :(得分:0)

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

这是一个软性受限许可,除非记录中的安装程序将许可列入白名单,否则该应用不能以其完整格式保留该许可。具体而言,如果将许可列入白名单,则持有人应用程序可以访问外部存储设备以及视觉和听觉媒体收藏,而如果未将许可列入白名单,则持有人应用程序只能访问视觉和听觉媒体收藏。此外,权限是一成不变的限制,这意味着白名单状态只能在安装时指定,并且在安装应用程序之前无法更改。有关更多详细信息,请参见PackageInstaller.SessionParams.setWhitelistedRestrictedPermissions(Set)