gpt4 book ai didi

android - Cordova 文件插件 : SECURITY_ERR

转载 作者:行者123 更新时间:2023-11-29 01:05:43 26 4
gpt4 key购买 nike

我有一个使用 Cordova 插件的混合应用程序 (iOS/Android)。我有一位 Android 用户在使用文件插件时收到 SECURITY_ERR。这似乎是在调用 writeFile() 时发生的。我为 Android 写入的文件路径是 externalRootDirectory

谁能帮助我理解为什么 300-400 名用户中有 1 名用户会遇到此问题?如果有帮助,用户使用的是 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
});
}

最佳答案

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

Marshmallow requires the apps to ask for permissions when reading/writing to external locations. By default, your app has permission to write to cordova.file.applicationStorageDirectory and cordova.file.externalApplicationStorageDirectory, and the plugin doesn't request permission for these two directories unless external storage is not mounted. However due to a limitation, when external storage is not mounted, it would ask for permission to write to cordova.file.externalApplicationStorageDirectory.

所以在 Android 6.0+ 上,当写入文件到磁盘时,Cordova File 插件会显示类似这样的提示:

Allow APP_NAME to access photos, media and files on your device?

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

关于android - Cordova 文件插件 : SECURITY_ERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47394639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com