Efraín
Efraín

Reputation: 505

Exception occurred while executing exported method askAsync on module ExpoPermissions: null

I'm having an error while using the ExpoPermission interface. I just started to learn about Android development with React and Expo, followed a guide to create a new project

npm install -g create-react-native-app 
create-react-native-app my-app 
cd my-app/ 
npm start

Everything worked, and the example run on my phone, then i just added a simple button that calls the example from the expo documentation, but it launches this message when i press it.

[Error: Encountered an exception while calling native method: Exception occurred while executing exported method askAsync on module ExpoPermissions: null]

My code is simple, just added to the view

<TouchableOpacity onPress={handlePressMe} style={styles.helpLink}>
  <Text style={styles.LinkBlue}>
    Press me!
  </Text>
</TouchableOpacity>

Then added the code:

function handlePressMe() {
  request_storage_runtime_permission();
}

async function request_storage_runtime_permission() {

  try {
    const granted = await Permissions.askAsync(Permissions.WRITE_EXTERNAL_STORAGE);
    print(granted)
    Alert.alert("Storage Permission received.");
    /*if (granted === PermissionsAndroid.RESULTS.GRANTED) {

      Alert.alert("Storage Permission Granted.");
    }
    else {

      Alert.alert("Storage Permission Not Granted");

    }*/
  } catch (err) {
    console.warn(err)
  }
}

And import * as Permissions from 'expo-permissions';

I've read the documentation of Expo, and found many installs that said to do for this library, but none has changed anything, just in case, both things i've added

sudo expo install expo-permissions
sudo yarn add react-native-unimodules

Also to note that i added the permission to app.json, and tried to use getAsync to see if it had been granted, but launched the same error, this is my app.json:

{
  "expo": {
    "name": "Test App",
    "slug": "testApp",
    "privacy": "public",
    "sdkVersion": "35.0.0",
    "platforms": [
      "android",
      "web"
    ],
    "version": "1.0.0",
    "orientation": "portrait",
    "icon": "./assets/images/icon.png",
    "splash": {
      "image": "./assets/images/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "android": {
      "permissions": [
        "WRITE_EXTERNAL_STORAGE"
      ]
    }
  }
}

Tried searching for more info, but only thing i found are unanswered topics. Does anyone knows what is the problem, or how to prompt more info? Only thing it shows is null.

Upvotes: 0

Views: 3695

Answers (1)

Efra&#237;n
Efra&#237;n

Reputation: 505

After trying to do it directly with React Native, i found the problem here:

Expo uses it's own way to name permissions, so if you want to request for WRITE_EXTERNAL_STORAGE, while on RN you would call exactly for this permission, on Expo you need to ask for CAMERA_ROLL permission instead, which includes both Write and Read permissions. The error message is little intuitive, since it only marks null, while trying to say that you're sending a null as parameter due to the permission that you are requesting (Permissions.WRITE_EXTERNAL_STORAGE) returning null. So the solution was to change it to:

const granted = await Permissions.askAsync(Permissions.CAMERA_ROLL);

Hope this helps someone on the future.

Upvotes: 1

Related Questions