Cordova 文件插件:SECURITY_ERR
Cordova File Plugin: SECURITY_ERR
我有一个正在使用 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 after Android 6.0 certain permissions must be requested during use of the app (not just at install time). This is also alluded to in the Cordova File plugin docs,在 Android 怪癖下。
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 文件插件将显示类似于以下的提示:
Allow APP_NAME to access photos, media and files on your device?
如果用户选择拒绝此请求,则 Cordova 文件写入将获得此 SECURITY_ERR
,即使应用程序在安装时请求此权限。
我有一个正在使用 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 after Android 6.0 certain permissions must be requested during use of the app (not just at install time). This is also alluded to in the Cordova File plugin docs,在 Android 怪癖下。
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 文件插件将显示类似于以下的提示:
Allow APP_NAME to access photos, media and files on your device?
如果用户选择拒绝此请求,则 Cordova 文件写入将获得此 SECURITY_ERR
,即使应用程序在安装时请求此权限。