From 871e924907df2342c00d178a4d5311e368a537b7 Mon Sep 17 00:00:00 2001 From: Joshua Pinter Date: Thu, 10 Dec 2020 16:01:26 -0700 Subject: [PATCH] Check and requets for read permission on Android. This is required on Android 10+. Otherwise, no images or videos will be shown. Also confirmed to be working on Android 5, which doesn't show the request if the permission is already added to the app's AndroidManifest file. The check and request method was pulled directly from cameraroll's README, with the exception of just checking the READ permission instead of the WRITE permission: https://github.com/react-native-cameraroll/react-native-cameraroll#permissions. --- index.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 16b215c..b25af9a 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,12 @@ import React, { Component } from 'react'; import { + ActivityIndicator, + FlatList, + PermissionsAndroid, Platform, StyleSheet, - View, Text, - FlatList, - ActivityIndicator, + View, } from 'react-native'; import CameraRoll from "@react-native-community/cameraroll"; import PropTypes from 'prop-types'; @@ -70,7 +71,11 @@ class CameraRollPicker extends Component { this.renderImage = this.renderImage.bind(this); } - componentWillMount() { + async componentWillMount() { + if (Platform.OS === "android" && !(await this.hasAndroidPermission())) { + return; + } + this.fetch(); } @@ -80,6 +85,22 @@ class CameraRollPicker extends Component { }); } + // Ensure we have the correct permissions read external storage on Android (required for Android 10+). + // Pulled straight from cameraroll README. + // See https://github.com/react-native-cameraroll/react-native-cameraroll#permissions. + async hasAndroidPermission() { + console.log( "Hello there love!" ); + const permission = PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE; + + const hasPermission = await PermissionsAndroid.check(permission); + if (hasPermission) { + return true; + } + + const status = await PermissionsAndroid.request(permission); + return status === 'granted'; + } + onEndReached() { if (!this.state.noMore) { this.fetch();