Cordova文件插件:SECURITY_ERR

时间:2017-11-20 14:44:41

标签: android cordova cordova-plugin-file

我有一个使用Cordova插件的混合应用程序(iOS / Android)。我有一个Android用户在使用File插件时获得了SECURITY_ERR。这似乎是在致电writeFile()时发生的。我写给Android的文件路径是externalRootDirectory

任何人都可以帮助我理解为什么1个用户(300-400)会有这个问题吗?如果有帮助,用户将使用Android 6.0.1。

下面是一些代码。我得到的错误是[Error creating file] – Error Msg: [SECURITY_ERR],因此在这种情况下会遇到.writeFile()捕获。

  //Handle Native download
  if (this.appConfig.isNative) {
    this.loggingService.debug("Starting to create native file");
    //Get base file path for android/ios
    let filePath = (this.appConfig.isNativeAndroid) ? this.file.externalRootDirectory : this.file.cacheDirectory;

    //Write the file
    this.file.writeFile(filePath, fileName, data, { replace: true })
          .then((fileEntry: FileEntry) => {
              this.loggingService.debug("Created file: " + fileEntry.toURL());          
              //Open with File Opener plugin
              this.fileOpener.open(fileEntry.toURL(), data.type)
                .then(() => this.loggingService.debug('File is opened'))
                .catch(e => this.loggingService.error('Error openening file', e));
            })
          .catch((err) => {
             this.loggingService.error("Error creating file", err);
             throw err;  //Rethrow - will be caught by caller
          });
      }

1 个答案:

答案 0 :(得分:1)

我能够解决这个问题。在Android 6.0之后Looks like必须在使用应用程序期间请求某些权限(而不仅仅是在安装时)。在Android Quirks下的Cordova File plugin docs中也提到了这一点。

  

Marshmallow要求应用在读取/写入外部位置时要求权限。默认情况下,您的应用程序有权写入cordova.file.applicationStorageDirectory和cordova.file.externalApplicationStorageDirectory,除非未安装外部存储,否则插件不会请求这两个目录的权限。但是由于存在限制,当未安装外部存储时,它会请求写入cordova.file.externalApplicationStorageDirectory的权限。

因此,在Android 6.0+上,当将文件写入磁盘时,Cordova File插件将显示类似于以下内容的提示:

  

允许APP_NAME访问您设备上的照片,媒体和文件?

如果用户选择拒绝此请求,则Cordova文件写入将获得此SECURITY_ERR,即使应用程序在安装时请求此权限。