ionic externalRootDirectory无法写入SD卡

jcs*_*mit 3 filesystems android cordova ionic-framework

我一直尝试使用IONIC中的“ cordova-plugin-file”插件在Android设备上写入(可移动)SD卡,但未成功。

该文档指定了

externalRootDirectory
Run Code Online (Sandbox Code Playgroud)

作为“ Android:外部存储(SD卡)的根目录”。

但是,当我写入该目录时:

this.file.resolveDirectoryUrl( this.file.externalRootDirectory)
.then( (data)=>{
  result += "\n" + "resolveDirectoryUrl";
  newBasePath = data.nativeURL;
  this.file.createDir(newBasePath, newDirName, true)
  .then( ()=>{
    result += "\n" + "createDir";
    this.file.createFile(newBasePath, newFilePath, true)
    .then( ()=>{
      result += "\n" + "createFile";
      this.file.writeFile(newBasePath, newFilePath, this.thisRouteFile,  {append:true})
      .then( () => {
        result += "\n" + "writeFile OK";
      });
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

文件始终会写入内部存储器或仿真文件,而不是可移动SD卡。

关于这个问题的讨论很多,有的引用

  <preference name="AndroidPersistentFileLocation" value="Compatibility" />
Run Code Online (Sandbox Code Playgroud)

但我没有找到解决方法。

是否可以写入可移动SD卡?

Dav*_*den 5

TL; DR:使用cordova-diagnostic-plugin中getExternalSdCardDetails()函数。

返回的“外部目录” cordova-plugin-file对应于不可移动的(内部)存储。这是因为保证这些模拟位置始终在所有Android设备上存在,而并非所有硬件供应商都支持外部/可移动SD卡位置,并且特定于制造商/ Android版本,并且如果使用外部SD卡,则可能不存在未插入阅读器。

例如,在运行Android 7.1.1的三星Galaxy S4上:

  • cordova.file.externalRootDirectory 退货 file:///storage/emulated/0/
  • cordova.file.externalApplicationStorageDirectory 退货 file:///storage/emulated/0/Android/data/cordova.plugins.diagnostic.example/

它们是不可移动内部存储器上的文件路径。

相比之下,cordova-diagnostic-plugin中getExternalSdCardDetails()函数返回可移动外部SD卡的位置和详细信息。

例如,在运行Android 7.1.1的Samsung Galaxy S4上,它返回:

[{
    "path": "/storage/4975-1401/Android/data/cordova.plugins.diagnostic.example/files",
    "filePath": "file:///storage/4975-1401/Android/data/cordova.plugins.diagnostic.example/files",
    "canWrite": true,
    "freeSpace": 16254009344,
    "type": "application"
}, {
    "path": "/storage/4975-1401",
    "filePath": "file:///storage/4975-1401",
    "canWrite": false,
    "freeSpace": 16254009344,
    "type": "root"
}]
Run Code Online (Sandbox Code Playgroud)

它们是外部可移动存储上的文件路径。