我需要将文件写入sd卡的根目录。 我使用getExternalSdCardDetails()中的cordova-diagnostic-plugin函数。 得到这样的东西:
[{
"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"
}]
因此,我无法写file:///storage/4975-1401
,因为"canWrite": false
我添加了
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />
到config.xml
和
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
到AndroidManifest.xml
,但是没有结果,当我尝试将文件写入根目录时,出现错误CODE 5 ENCODING_ERR
正在写/storage/4975-1401/Android/data/cordova.plugins.diagnostic.example/files
。
答案 0 :(得分:0)
自Android 4.4 Kitkat起,非系统应用对其应用沙箱区域(/Android/data/[package-id]/
)以外的辅助存储卷(即可移动SD卡)具有只读权限。 See here以获得更详细的说明。
因此getExternalSdCardDetails()
函数正确地通知您可以写入应用程序沙箱目录/storage/4975-1401/Android/data/cordova.plugins.diagnostic.example/files
,但不能写入根目录/storage/4975-1401/
。
除非设备已植根,否则在Android 4.4及更高版本上无法解决此问题。