使用Cordova FileTransfer将文件保存到公用目录 [英] Save file to public directory using Cordova FileTransfer

查看:474
本文介绍了使用Cordova FileTransfer将文件保存到公用目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的移动设备上下载文件,并允许其他应用访问其他应用(使用Android和iOS)。

I need to download files on my mobile device and make them accessible for other apps (using Android and iOS).

我设法将文件下载到SD卡( cordova.file.externalDataDirectory ),但这仅存在于Android上,即使我不能依赖每个具有SD卡的设备。

I managed to download a file to the SD card (cordova.file.externalDataDirectory), but this only exists on Android and even then I cannot rely on every device having an SD card.

当我下载到设备存储( cordova.file.dataDirectory )时,该文件对我的应用程序是不公开的,因此无法访问其他应用程序。该文件可以在InAppBrowser中打开,但我希望使用相应的默认应用程序。

When I download to the device storage (cordova.file.dataDirectory), the file is private to my app and therefore not accessible for other apps. The file can be opened in the InAppBrowser, but I would prefer to use the respective default app.

有一种方法可以公开获取目录的路径在所有设备上可用?

http://stackoverflow.com/a/21375812/3432305 在Android上都是私人的...

The paths returned by the solution suggested in http://stackoverflow.com/a/21375812/3432305 are both private on Android...

编辑:

我想我应该描述我的用例,以便更清楚我想实现:我想在应用程序聊天中从打开文件<使用相应的默认应用程序(pdf查看器,图像查看器等)。由于 仅接受来自本地文件系统的文件,我需要先保存它们。但

I think I should describe my use case so it's clearer what I'm trying to achieve: I want to open files from my in app chat using the respective default app (pdf viewer, image viewer etc.). Because the Cordova File Opener plugin only accepts files from the local file system, I need to save them first. But they don't necessarily need to be accessible from outside my app afterwards...

推荐答案

在Android上,外部存储目录总是存在;如果设备没有物理SD卡,Android将会模拟它。请参阅 getExternalStorageDirectory

On Android, external storage directories always exist; if the device doesn't have a physical SD card, Android will emulate it. see getExternalStorageDirectory :


注意:不要在这里混淆external一词。这个目录
可以更好地被认为是媒体/共享存储。它是一个文件系统,
可以容纳相对大量的数据,并且跨
所有应用程序共享(不强制执行权限)。传统上,这是一个SD卡的
,但它也可以实现为
设备中的内置存储,它不同于受保护的内部存储,可以
作为文件系统安装在

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

Therefore cordova.file.externalDataDirectory will always resolve. However, for sharing data between apps, you probably want to use cordova.file.externalRootDirectory - External storage (SD card) root. See cordova-plugin-file.

这样,您可以将文件存储在更容易从其他应用访问的位置。 / sdcard / my_shared_data /

This way you can store files in a place that's easier to access from another app e.g. /sdcard/my_shared_data/

在iOS上,共享文件更加困难,因为应用程序是有意隔离的由于安全政策,如Apple的应用程式间通讯指南说:

On iOS, it's more difficult to share files because apps are intentionally isolated from each other due to security policy, as Apple's Inter-App Communication Guide says:


应用程式只能间接与装置上的其他应用程式通讯

Apps communicate only indirectly with other apps on a device

你最好的选择是iOS是通过iCloud同步数据来共享数据。请参阅在iCloud设计指南中为多个应用程序配置一个通用的Ubiquity容器,它说:

You best bet on iOS is to share the data by synching it via iCloud. See the section Configuring a Common Ubiquity Container for Multiple Apps in iCloud Design Guide, which says:


...也许你提供两个互动的应用程序,需要
访问对方的文件。在这两个示例中,您通过指定一个公共无处不在的容器,然后
请求从每个应用程序访问它来获得
所需的访问。

... 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.

这篇关于使用Cordova FileTransfer将文件保存到公用目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆