简体   繁体   中英

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

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" />

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.

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 Developer documentation

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. 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:

<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. 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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM