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

查看:2621
本文介绍了使用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卡(

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.

有没有办法公开路径到目录在所有设备上可用?

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

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

编辑:

我想我应该描述我的用例,所以我想要实现的更清楚:我想从我的应用聊天中的打开文件使用相应的默认应用程序(pdf查看器,图像查看器等)。因为 Cordova File Opener plugin 只接受来自本地文件系统的文件,我需要先保存它们。但是他们并不一定需要从我的应用程序之外访问...

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 :


注意:不要在这里用外部这个词混淆。该目录
可以更好地被认为是媒体/共享存储。它是一个文件系统,
可以容纳相对大量的数据,并且在
所有应用程序之间共享(不执行权限)。传统上,这是一个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上,共享文件更加困难,因为应用程序是故意隔离的由于安全政策,因为苹果的应用间通信指南说:

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天全站免登陆