gpt4 book ai didi

android - cordova fs.root.getDirectory 在三星安卓手机上失败(PATH_EXISTS_ERR)

转载 作者:行者123 更新时间:2023-12-05 07:40:50 29 4
gpt4 key购买 nike

此 Cordova 代码适用于 iOS 和大多数 Android 设备,但在某些三星设备上失败:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem, gotFileError); 

function gotFileSystem(fs)
{
fs.root.getDirectory(
'my_folder',
{create: true},
function (entry) {
// Yay! Got the directory,
// unless running certain Samsung Android devices,
// in which case the "gotFileError" function will
// run with the error FileError.PATH_EXISTS_ERR
},
gotFileError
);

});

三星怎么了?
或者,更重要的是,我如何才能在所有 Samsung 设备上也使用此功能?

最佳答案

问题

显然在 Android 6.0.0 及更高版本中,应用程序必须明确请求权限才能写入文件系统 (ref),并且所有现有应用程序都可能会崩溃。
我检查了我所拥有的失败设备的 Android 版本,均为 6.0.1。

修复

1. 安装“cordova-diagnostic-plugin
$ cordova 插件添加 cordova.plugins.diagnostic

2. 在调用 fs.root.getDirectory() 之前添加此逻辑

function gotFileSystemButNowCheckForWritePermission(fs)
{
cordova.plugins.diagnostic.requestRuntimePermission(
function(status){
switch(status){
case cordova.plugins.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted");
gotFileSystemAndAccessPermission(fs);
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED:
console.log("Permission denied");
alert('App needs access to the phones file storage in order to run');
gotFileSystemButNowCheckForWritePermission(fs);
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied");
alert('Cannot run App without access to the phones file storage');
break;
}
},
function(error){
console.error("The following error occurred: "+error);
},
cordova.plugins.diagnostic.permission.WRITE_EXTERNAL_STORAGE
);
}

(特别感谢这个答案:https://stackoverflow.com/a/45779916/1290746)


更多信息

所有应用程序,甚至 Facebook 和 Twitter,现在都需要这样做。事实上,他们经常在 Android 系统消息之前添加额外的消息以获取权限:

https://www.androidcentral.com/run-permissions-why-change-android-60-may-make-you-repeat-yourself

另请参阅:http://www.androidpolice.com/2015/09/07/android-m-begins-locking-down-floating-apps-requires-users-to-grant-special-permission-to-draw-on-other-apps/

"Android M may soon earn a reputation for an overabundance of confirmation dialogs, at least that's how it will feel to anybody setting up a new device. Fortunately, we need only grant each permission to an app once, so things will never get as bad as Windows Vista."

技术信息:https://developer.android.com/training/permissions/requesting.html更多信息:https://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal

根据这个:https://medium.com/prodgasm/obtaining-app-permissions-the-right-way-and-humanizing-the-process-1c9e2d6d5818 : 可以在提示中添加一些额外的文本。

关于android - cordova fs.root.getDirectory 在三星安卓手机上失败(PATH_EXISTS_ERR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45837433/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com