如何确保我的Android 6 Cordova应用程序在尝试访问文件系统之前提示用户授予权限? [英] How can I ensure that my Android 6 Cordova app prompts the user to give permission before attempting to access the filesystem?

查看:4043
本文介绍了如何确保我的Android 6 Cordova应用程序在尝试访问文件系统之前提示用户授予权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Cordova 3.4应用程序升级到Cordova 6.0,同时将部署平台(Nexus平板电脑)从Android 4.4升级到Android 6.作为其中的一部分,我升级了文件系统插件 cordova-plugin-file 从v0.2.5到v4.1.1

I am upgrading a Cordova 3.4 application to Cordova 6.0 at the same time as upgrading the deployment platform (Nexus tablets) from Android 4.4 to Android 6. As part of this I've upgraded the filesystem plugin cordova-plugin-file from v0.2.5 to v4.1.1

应用程序创建共享文件系统上的10个文件夹的目录结构code> file:/// storage / emulated / 0 / )。我添加了以下首选项到config.xml以确保在新版本的应用程序中使用相同的位置:

The application creates a directory structure of 10 folders on the shared filesystem (file:///storage/emulated/0/) upon first run. I've added the following preference to config.xml to ensure the same location is used in the new version of the app:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />

我知道在Android 6中有一个新的权限模型,用户必须单独允许应用程序请求的每个权限。

I'm aware that there is a new permission model in Android 6 where the user has to separately allow each permission that the application requests.


如果设备运行的是Android 6.0(API级别23)或更高版本,而
应用的targetSdkVersion为23或更高,应用程序在运行时向用户请求权限

Android开发人员文档

当安装在安装了早期版本(并且自从卸载)并且已创建目录结构的平板电脑上之后,新版本的应用程序执行为预期在第一次运行时提示用户允许访问文件系统。

When installed on tablets that have had an earlier version installed (and since uninstalled) and which have had the directory structure created, the new version of my app performs as expected upon first run, prompting the user to allow permission to access the file system.

但是,当安装在从未安装应用程序的平板电脑上时我删除了由早期版本创建的目录结构)应用程序失败通过引发 FileError.PATH_EXISTS_ERR 错误。它不会提示用户允许访问文件系统的预期。

However, when installed upon a tablet which has never had the app installed before (or when I remove the directory structure created by an earlier version) the app fails by throwing the FileError.PATH_EXISTS_ERR error. It does not prompt the user to allow permission to access the file system as expected.

我已经检查,当应用程序构建时,生成的AndroidManifest.xml确实包含必要的权限属性:

I've checked that when the application is built, the generated AndroidManifest.xml does contain the necessary permissions attribute:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我当前的解决方法是停止应用程序,导航到设置>应用程序> MyApp>权限并手动启用存储权限,然后再次运行应用程序。但我真的不希望我的用户必须这样做!

My current workaround is to stop the app, navigate to Settings > Apps > MyApp > Permissions and manually enable the "Storage" permission before running the app again. But I really don't want my users to have to do this!

如何确保应用程序提示用户在实际尝试之前提供所需的权限访问文件系统?

How can I ensure that the app does prompt the user to give the required permission before actually attempting to access the filesystem?

以下是我的代码示例:

window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 4000 * 1024 * 1024, gotFileSystem, errorHandler);

function gotFileSystem(fileSystem) {

    // store reference to file system
    fs = fileSystem;
}

function createDir(directoryName) {

    var directoryCreated = when.defer();

    fs.root.getDirectory(directoryName, {
            create: true
        }, function (dirEntry) {            
            directoryCreated.resolve();
        }, function(e) {
            directoryCreated.reject(e);
        }
    );

    return directoryCreated.promise;
}


推荐答案

升级到Android 6(MArshmallow)。 Cordova文件插件(v 4.1.1)有Android Marshmallow的权限问题。这个问题不会允许在android文件系统中创建文件夹,导致路径存在错误。

此问题现已解决。你可能需要从android src文件夹中cordova文件插件github拉最新的FileUtils.java,使这项工作。更新插件后,首次访问文件系统时,应用程序会向用户请求访问权限。随后的请求从那时开始工作正常。检查CB-10798在Apache问题跟踪器的信息。希望它有帮助

Faced the exact same issue after upgrading to Android 6 (MArshmallow). Cordova file plugin (v 4.1.1) has permission issue with Android Marshmallow. This issue will not allow folders to be created in android filesystem resulting in path exist error.

This issue is fixed now. You may have to pull the latest FileUtils.java from android src folder in cordova file plugin github to make this work. Once you updated the plugin, at first access to file system, the app requests for the access permission from user. Subsequent requests works fine from then on. Check CB-10798 in Apache issue tracker for info on this. Hope it helps

这篇关于如何确保我的Android 6 Cordova应用程序在尝试访问文件系统之前提示用户授予权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆