我需要在我的移动设备上下载文件并让其他应用程序可以访问它们(使用 Android 和 iOS)。
我设法将文件下载到 SD 卡 (cordova.file.externalDataDirectory),但这只存在于 Android 上,即便如此我也不能依赖每个设备都有 SD 卡。
当我下载到设备存储 (cordova.file.dataDirectory) 时,该文件对我的应用程序是私有(private)的,因此其他应用程序无法访问。该文件可以在 InAppBrowser 中打开,但我更愿意使用相应的默认应用程序。
有没有办法获取所有设备上公开可用的目录路径?
https://stackoverflow.com/a/21375812/3432305 中建议的解决方案返回的路径在 Android 上都是私有(private)的...
编辑:
我想我应该描述我的用例,以便更清楚我想要实现的目标:我想使用相应的默认应用程序(pdf 查看器、图像查看器)从我的应用程序聊天中打开文件ETC。)。因为Cordova File Opener plugin只接受来自本地文件系统的文件,我需要先保存它们。但之后它们不一定需要从我的应用程序外部访问...
最佳答案
在Android上,外部存储目录总是存在的;如果设备没有物理 SD 卡,Android 将模拟它。见getExternalStorageDirectory :
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
因此 cordova.file.externalDataDirectory
将始终解析。但是,为了在应用程序之间共享数据,您可能需要使用 cordova.file.externalRootDirectory
- 外部存储(SD 卡)根目录。参见 cordova-plugin-file .
这样您就可以将文件存储在一个更容易从其他应用访问的地方,例如/sdcard/my_shared_data/
在 iOS 上,共享文件更加困难,因为应用程序由于安全策略而有意彼此隔离,如 Apple 的 Inter-App Communication Guide说:
Apps communicate only indirectly with other apps on a device
在 iOS 上最好的选择是通过 iCloud 同步数据来共享数据。请参阅 Configuring a Common Ubiquity Container for Multiple Apps 部分在 iCloud 设计指南中,它说:
... perhaps you provide two apps that interoperate and need access to each other’s files. In both of these examples, you obtain the needed access by specifying a common ubiquity container and then requesting access to it from each app.
关于android - 使用 Cordova FileTransfer 将文件保存到公共(public)目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33076885/