我用过Cordova File Plugin在移动设备上创建文件。下面是创建文件的代码:
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) {
alert(cordova.file.dataDirectory);
dir.getFile("log.txt", { create: true }, function (file) {
alert("got the file: "+ file.name + ', ' + file.fullPath);
});
});
当我在我的安卓手机上部署应用程序时,文件会创建成功,但我在我的设备上找不到创建的文件。
虽然 cordova.file.dataDirectory
指向我设备上的 file:///data/data/io.cordova.myappId/files/
路径,但 io.cordova.myappId
文件夹在 data>data
路径中不存在,但在 Android>data
路径中存在。顺便说一句,我检查了 storage>Android>data>io.Cordova.myappId>files
和 storage>data>data
并且该文件不存在。
这是因为:
创建的文件位于另一个地方,那么我在哪里可以找到呢?
或
因为它是私有(private)的,而我的文件管理员无权访问它,所以我该如何更改权限设置以拥有公共(public)文件?
最佳答案
为什么在我的设备上找不到文件?
文件已成功创建,但我在我的设备上找不到该文件,因为 dataDirectory
我指示创建文件的路径是私有(private)路径,我的设备文件管理器无权访问它(基于 this table )。其实dataDirectory
是 Internal Storage
选项。
Internal Storage: Store private data on the device memory.
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.(reference)
如何创建公共(public)文件?
因此,要在我的设备上创建一个可以使用文件管理器访问的文件,我必须使用以下公共(public)路径选项之一:externalDataDirectory
.之前,我认为它是用于将文件存储在我手机上没有的外部 SD 卡上,所以我没有测试它。但是今天测试它,它在我的内部设备存储中创建文件 Android/data/<app-id>/files
路径是公开的,我可以使用设备文件管理器访问它。
其实是internal
这个词和 external
在 external storage
时误导了我可以是可移动存储介质(例如 SD 卡)或内部(不可移动)存储 (reference)。
关于android - Cordova File Plugin 创建的文件的位置在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694139/