简体   繁体   English

使用cordova和文件插件强制Android上的外部SD卡

[英]Forcing external sdcard on android with cordova and file plugin

In a cordova android app (scroll to the bottom for device,version, plugin list, all of it quite up-to-date as of this writing) I want to preferentially store file on the external SDCard and use internal storage if SDCard not there. 在一个cordova Android应用程序(滚动到底部的设备,版本,插件列表,所有这些都是最新的写作)我想优先存储外部SDCard上的文件并使用内部存储,如果SDCard不存在。 I am setting my save path to: 我将保存路径设置为:

persistentFS= cordova.file.externalDataDirectory||cordova.file.DataDirectory||fileSystem.root.toURL();

Its eventual value is file:///storage/emulated/0/Android/data/com.fubar.app/files/, same as cordova.file.externalDataDirectory. 它的最终值是file:///storage/emulated/0/Android/data/com.fubar.app/files/,与cordova.file.externalDataDirectory相同。

I have set: <access origin="cdvfile://*" /> ... <preference name="AndroidPersistentFileLocation" value="Compatibility" /> <preference name="AndroidExtraFilesystems" value="files-external,sdcard,files,documents,cache,cache-external,root" /> in config.xml, <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 我设置: <access origin="cdvfile://*" /> ... <preference name="AndroidPersistentFileLocation" value="Compatibility" /> <preference name="AndroidExtraFilesystems" value="files-external,sdcard,files,documents,cache,cache-external,root" /> config.xml中的<access origin="cdvfile://*" /> ... <preference name="AndroidPersistentFileLocation" value="Compatibility" /> <preference name="AndroidExtraFilesystems" value="files-external,sdcard,files,documents,cache,cache-external,root" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in AndroidManifest.xml and set various enchantments in index.html Security metatag. 在AndroidManifest.xml中并在index.html Security metatag中设置各种附魔。 I add that a SDcard is inserted, write-enabled and I confirm that I can write to it with - for example - a file manger. 我添加了一个SD卡插入,写入启用,我确认我可以写入 - 例如 - 文件管理器。 It contains the Android/data/com.fubar.myapp/files/ folder, as it should. 它包含Android / data / com.fubar.myapp / files /文件夹。

All for nothing: files get written to - and read from - the internal device storage. 一切都是:文件被写入 - 并从内部设备存储读取。 I have read all google scavenged on this topic but nothing has brought me closer to the task (some folks suggest using the file-system-roots plugin, but it appears to have been subsumed by the file plugin). 我已经阅读了关于这个主题的所有谷歌,但没有任何东西让我更接近任务(一些人建议使用文件系统根插件,但它似乎已被文件插件包含)。

Any hints? 任何提示? TIA, alf TIA,alf

Edit: Using adb shell, I can see that the external sdcard files folder for the app has an absolute path of: 编辑:使用adb shell,我可以看到应用程序的外部sdcard文件文件夹的绝对路径为:

`/storage/extSdCard/Android/data/com.fubar.myapp/files`

and calling resolveLocalFileSystemURL on 并调用resolveLocalFileSystemURL

file:///storage/extSdCard/Android/data/com.fubar.myapp/files

happily succeeds. 愉快地成功。 I do not think that is the way to go, as that path is device dependent. 我不认为这是要走的路,因为该路径取决于设备。


Device: Samsung Galaxy tab 10.5, lollipop 5.0.1 using cordova 5.3.1, with plugins: cordova-plugin-device 1.0.1 "Device" cordova-plugin-dialogs 1.1.1 "Notification" cordova-plugin-file 3.0.0 "File" cordova-plugin-file-transfer 1.3.0 "File Transfer" cordova-plugin-media 1.0.1 "Media" cordova-plugin-whitelist 1.1.0 "Whitelist" 设备:Samsung Galaxy tab 10.5,lollipop 5.0.1使用cordova 5.3.1,带插件:cordova-plugin-device 1.0.1“Device”cordova-plugin-dialogs 1.1.1“Notification”cordova-plugin-file 3.0.0 “文件”cordova-plugin-file-transfer 1.3.0“文件传输”cordova-plugin-media 1.0.1“媒体”cordova-plugin-whitelist 1.1.0“白名单”

Build host is Linux FC21 构建主机是Linux FC21

You can call Context.getExternalFilesDirs() . 您可以调用Context.getExternalFilesDirs() That returns an array of directories to which your app can write data. 这将返回一个应用程序可以写入数据的目录数组。 One of which will be the sdcard path /storage/sdcardname/Android/data/appname/files. 其中一个是sdcard路径/ storage / sdcardname / Android / data / appname / files。

Most manufacturers also set the SECONDARY_STORAGE environment variable to expose the SD Card root path. 大多数制造商还设置SECONDARY_STORAGE环境变量以公开SD卡根路径。 You can try calling System.getenv("SECONDARY_STORAGE") 您可以尝试调用System.getenv("SECONDARY_STORAGE")

Android does not expose any API that allows you to get the path of the sdcard root path. Android不会公开任何允许您获取SD卡根路径的API。 You can use reflection and obtain the sdcard root path from StorageManager.java -> getVolumePaths() - http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/storage/StorageManager.java#590 您可以使用反射并从StorageManager.java获取SD卡根路径 - > getVolumePaths() - http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/storage/StorageManager。 Java的#590

Since there doesn't seem to be a true Cordova answer to this yet here is what I found using the file plugin . 由于似乎没有真正的Cordova答案,这里是我发现使用文件插件 I was also looking to save on the SD card as well as the data will later be attached to email and Gmail now requires an attachment to be loaded from SD. 我还希望保存在SD卡上,以及稍后将数据附加到电子邮件中,Gmail现在需要从SD加载附件。 This blog got me going in the right direction https://www.neontribe.co.uk/cordova-file-plugin-examples/ . 这个博客让我走向正确的方向https://www.neontribe.co.uk/cordova-file-plugin-examples/ In the section talking about writing a file basically just replace 在谈论编写文件的部分基本上只是替换

window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (directoryEntry) {

with this 有了这个

window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (directoryEntry) {

And it should be working. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM