我一直在努力尝试使用" cordova-plugin-file"写入Android设备上的(可移动)SD卡。插入IONIC。
文档指定
externalRootDirectory
as" 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";
});
});
});
});
文件始终写入内部存储器或仿真,而不是可移动SD卡。
有很多关于这个问题的讨论,有些引用
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
但我没有找到解决方案。
是否可以写入可移动SD卡?
答案 0 :(得分:2)
TL; DR:使用getExternalSdCardDetails()中的cordova-diagnostic-plugin功能。
&#34;外部目录&#34;由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/
是不可移动内部存储上的文件路径。
相比之下,来自getExternalSdCardDetails()的cordova-diagnostic-plugin函数会返回可移动外部SD卡的位置和详细信息。
例如,在运行Android 7.1.1的三星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"
}]
是外部可移动存储上的文件路径。
答案 1 :(得分:1)
我认为您可能会错过访问外部存储的权限。
cordova-plugin-file插件为AndroidManifest.xml添加了一些行来请求android.permission.WRITE_EXTERNAL_STORAGE权限,但这已经不再适用于新的Android权限检查机制。
从Android 6开始,您必须在运行时请求权限,而不必仅将其放入清单。
这意味着每次调用需要特殊权限的函数时,都必须通过调用Android函数来确保您拥有该权限。
您可以使用Android-permissions plugin在运行时请求WRITE_EXTERNAL_STORAGE权限。