简体   繁体   English

在尝试访问文件系统之前,如何确保我的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?

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

The application creates a directory structure of 10 folders on the shared filesystem ( file:///storage/emulated/0/ ) upon first run. 首次运行时,应用程序在共享文件系统( file:///storage/emulated/0/ )上创建10个文件夹的目录结构。 I've added the following preference to config.xml to ensure the same location is used in the new version of the app: 我已经在config.xml中添加了以下首选项,以确保在新版本的应用程序中使用相同的位置:

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

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中有一个新的权限模型,用户必须单独允许应用程序请求的每个权限。

If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the app requests permissions from the user at run-time. 如果设备运行的是Android 6.0(API级别23)或更高版本,并且应用程序的targetSdkVersion为23或更高,则应用程序会在运行时请求用户的权限。 Android Developer documentation 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. 当安装在已安装早期版本(并且已卸载)且已创建目录结构的平板电脑上时,我的应用程序的新版本在首次运行时按预期执行,提示用户允许访问文件系统的权限。

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. 但是,当安装在从未安装过应用程序的平板电脑上时(或者当我删除早期版本创建的目录结构时),应用程序会因抛出FileError.PATH_EXISTS_ERR错误而失败。 It does not prompt the user to allow permission to access the file system as expected. 它不会提示用户允许按预期访问文件系统的权限。

I've checked that when the application is built, the generated AndroidManifest.xml does contain the necessary permissions attribute: 我已经检查过,在构建应用程序时,生成的AndroidManifest.xml确实包含必要的权限属性:

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

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. 我目前的解决方法是停止应用,导航到设置>应用> MyApp>权限,并再次运行应用之前手动启用“存储”权限。 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? 在实际尝试访问文件系统之前,如何确保应用程序确实提示用户提供所需的权限?

Here are some samples of my code: 以下是我的代码的一些示例:

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;
}

Faced the exact same issue after upgrading to Android 6 (MArshmallow). 升级到Android 6(MArshmallow)后面临完全相同的问题。 Cordova file plugin (v 4.1.1) has permission issue with Android Marshmallow. Cordova文件插件(v 4.1.1)与Android Marshmallow存在权限问题。 This issue will not allow folders to be created in android filesystem resulting in path exist error. 此问题不允许在android文件系统中创建文件夹,从而导致路径存在错误。

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. 您可能必须从cordova文件插件github中的android src文件夹中提取最新的FileUtils.java才能使其正常工作。 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. 检查Apache问题跟踪器中的CB-10798以获取相关信息。 Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何授予我的Android应用根权限? - How can I give root permission to my Android app? 如何确保我的Android应用不会同时访问文件? - How can I ensure that my Android app doesn't access a file simultaneously? 如何使用cordova授予android中文件的权限 - How to give permission to files in android using cordova 如何为我的App android设置默认短信权限? - How can i set default SMS permission to my App android? 如何确保在我的活动被杀之前完成AsyncTask? - How can I ensure an AsyncTask is completed before my activity is killed? 如何确保我的密钥库信息不会随我的cordova应用程序而改变 - How do I ensure my keystore info doesn't change with my cordova app 如何从Android应用程序访问用户安装的密钥和证书? - How can I access user-installed keys and certificates from my Android app? 无法将互联网访问我的Android应用 - Can't give access internet to my Android App 如何确保我的应用程序与每台Android设备兼容,并将视图缩放以适​​合不同的屏幕尺寸? - How can I ensure that my app is compatible with every Android device and will scale views to fit different screen dimensions? 如何确保我的应用仅适用于Android电子市场上的手机? - How can I ensure that my app is only available to phones on Android Market?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM