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

时间:2017-08-23 10:39:46

标签: android cordova samsung-mobile cordova-plugin-file

此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
    );

});

三星有什么用? 或者,更重要的是,我如何才能在所有三星设备上使用它?

1 个答案:

答案 0 :(得分:0)

问题

显然,在Android 6.0.0及更高版本中,已经确定应用必须明确要求允许写入文件系统(ref)并且所有现有应用都可以中断。
我检查了Android的版本,因为我失败的设备都是6.0.1。

修复

1。安装" cordova-diagnostic-plugin"
   $ cordova plugin add 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很快就会因为过剩而获得声誉   确认对话框,至少是对任何人的感受   设置新设备。幸运的是,我们只需要授予每个   对应用程序的权限一次,所以事情永远不会像Windows那样糟糕   Vista和#34;

技术信息: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:可以在提示中添加一些额外的文字。