How can I ensure that my Android 6 Cordova app prompts the user to give permission before attempting to access the filesystem?

finchie

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I give an App Registration permission to access Azure KeyVault?

How can I give my Google Cloud App Engine access to my Firebase Storage bucket

How can I give write permission to a specified user in Firebase?

how to give access to other user can access my account?

How to give permission to files in android using cordova

How I can give permission of written my folder in ubuntu?

How can I ensure my app can not be installed on Marshmallow

How can I ensure my Android app always starts on the main activity, even if it crashes?

How can I force user to update my android app with Github?

How can I ensure the integrity of my iOS app?

How can I give access to key vault to a user assigned identity?

I want to give access to users of their own dropbox from my java web app. How can I do that?

How can I access to my app with WildFly

How can I find out if my user has an admin permission?

Swift: Firebase: How to ensure no one can access my db except my app

Cannot give permissions to sign-in to my app, error "App needs permission to access resources in your organisation that only an admin can grant"

How can I give grafana user appropriate permission so that it can start successfully?

How can I access localStorage of Cordova App via WebdriverIO and Appium?

How I can launch my app in startup device with cordova?

How can i update a cordova lib for my app (2.1.0 to 5.1.1)

How do I create a single permission check in my android app?

how can I ensure a user will not delete another user object in my website [Django]

How can i make user access control in my existing Angularjs app

How to give access to my api for a mobile app?

How can I give others permission to read my INFORMATION_SCHEMA.SCHEMATA in BigQuery?

How can I ask for external file access permission in Android Q?

How can I access the Ubuntu filesystem in Windows?

How can I access anbox filesystem?

I can't access my javascript code because of infinite prompts