【发布时间】:2018-05-03 19:51:07
【问题描述】:
我有一个使用 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
});
}
【问题讨论】:
-
您是否在 config.xml 中定义了AndroidExtraFilesystems-preference?应将权限 (android.permission.WRITE_EXTERNAL_STORAGE) 插入 AndroidManifest.xml 并确保该目录确实存在,如果不创建它,例如 this
-
谢谢@Blauharley。我没有定义任何 AndroidExtraFilesystems 首选项。对于我所做的任何其他测试,我似乎都不需要它。我们有几百人毫无问题地使用它。 AndroidManifest.xml 中有一个
android.permission.WRITE_EXTERNAL_STORAGE权限。不确定是否默认添加了 File 插件?我也没有创建目录,而只是在externalRootDirectory的根目录中创建了一个临时文件。这种情况下还需要创建目录吗? -
在这种情况下,不,您不必创建目录,因为您已经在您想要的位置。您不会收到带有fileWriter 的 onerror 回调的特定错误消息吗?请在这个问题中插入一些代码。
-
可以@Blauharley - 我添加了一些代码细节。
-
我们现在有另外 1 份报告,来自 Android 7.0 用户。因此,我们已经在 800 多个用户中的 2-3 个用户身上看到了这一点。有什么想法吗?
标签: android cordova cordova-plugin-file