ionic externalRootDirectory 无法写入 SD 卡
ionic externalRootDirectory fails to write to SD card
我一直在尝试使用 IONIC 中的 "cordova-plugin-file" 插件写入 Android 设备上的(可移动)SD 卡,但没有成功。
文档指定
externalRootDirectory
作为"Android: the external storage (SD card) root."
但是,当我写入这个目录时:
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 卡?
我想你可能错过了访问外部存储的权限。
cordova-plugin-file 插件向 AndroidManifest.xml 添加行以请求 android.permission.WRITE_EXTERNAL_STORAGE 权限,但是对于新的 Android 权限检查机制,这已经不够用了。
从 Android 6 开始,您必须在 运行 时请求权限,而不仅仅是将其放入清单中。
这意味着每次调用需要特殊权限的函数时,您必须通过调用Android个函数来确保您拥有权限。
您可以在 运行 时使用 Android-permissions plugin 请求 WRITE_EXTERNAL_STORAGE 权限。
TL;DR:使用 getExternalSdCardDetails() function from cordova-diagnostic-plugin。
cordova-plugin-file
返回的"external directories"对应于不可移动(内部)存储。
这是因为这些模拟位置保证始终存在于所有 Android 设备上,而 external/removable SD 卡位置并非所有硬件供应商都支持,它们是制造商/Android 版本特定的, 如果没有插入外部 SD 卡,则可能不存在 reader.
例如,在三星 Galaxy S4 上 运行 Android 7.1.1:
cordova.file.externalRootDirectory
returns file:///storage/emulated/0/
cordova.file.externalApplicationStorageDirectory
returns file:///storage/emulated/0/Android/data/cordova.plugins.diagnostic.example/
这是不可移动内部存储上的文件路径。
相比之下,getExternalSdCardDetails() function from cordova-diagnostic-plugin returns 可移动外部 SD 卡的位置和详细信息。
例如,在三星 Galaxy S4 上 运行 Android 7.1.1 returns:
[{
"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"
}]
这是外部可移动存储上的文件路径。
我一直在尝试使用 IONIC 中的 "cordova-plugin-file" 插件写入 Android 设备上的(可移动)SD 卡,但没有成功。
文档指定
externalRootDirectory
作为"Android: the external storage (SD card) root."
但是,当我写入这个目录时:
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 卡?
我想你可能错过了访问外部存储的权限。
cordova-plugin-file 插件向 AndroidManifest.xml 添加行以请求 android.permission.WRITE_EXTERNAL_STORAGE 权限,但是对于新的 Android 权限检查机制,这已经不够用了。
从 Android 6 开始,您必须在 运行 时请求权限,而不仅仅是将其放入清单中。
这意味着每次调用需要特殊权限的函数时,您必须通过调用Android个函数来确保您拥有权限。
您可以在 运行 时使用 Android-permissions plugin 请求 WRITE_EXTERNAL_STORAGE 权限。
TL;DR:使用 getExternalSdCardDetails() function from cordova-diagnostic-plugin。
cordova-plugin-file
返回的"external directories"对应于不可移动(内部)存储。
这是因为这些模拟位置保证始终存在于所有 Android 设备上,而 external/removable SD 卡位置并非所有硬件供应商都支持,它们是制造商/Android 版本特定的, 如果没有插入外部 SD 卡,则可能不存在 reader.
例如,在三星 Galaxy S4 上 运行 Android 7.1.1:
cordova.file.externalRootDirectory
returnsfile:///storage/emulated/0/
cordova.file.externalApplicationStorageDirectory
returnsfile:///storage/emulated/0/Android/data/cordova.plugins.diagnostic.example/
这是不可移动内部存储上的文件路径。
相比之下,getExternalSdCardDetails() function from cordova-diagnostic-plugin returns 可移动外部 SD 卡的位置和详细信息。
例如,在三星 Galaxy S4 上 运行 Android 7.1.1 returns:
[{
"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"
}]
这是外部可移动存储上的文件路径。