1

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

});

三星怎么了?
或者,更重要的是,我怎样才能在所有三星设备上也能做到这一点?

4

1 回答 1

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 -其他应用/

“Android M 可能很快就会因确认对话框过多而声名狼藉,至少任何设置新设备的人都会有这种感觉。幸运的是,我们只需将每个权限授予应用程序一次,因此事情永远不会变得如此糟糕作为 Windows Vista。”

技术信息:https ://developer.android.com/training/permissions/requesting.html 更多信息:https ://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully -处理权限删除

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

于 2017-08-23T12:47:26.197 回答