Last active
June 21, 2024 09:05
-
-
Save YashDubeyOfficial/fa916c9c3524a88d6b6b38f10a0ff999 to your computer and use it in GitHub Desktop.
Android 14 storage permission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> | |
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/> | |
<!-- Devices running Android 12L (API level 32) or lower --> | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<!-- Devices running Android 13 (API level 33) or higher --> | |
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> | |
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" /> | |
<!-- To handle the reselection within the app on Android 14 (API level 34) --> | |
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED" /> | |
<application | |
android:requestLegacyExternalStorage="true"> | |
... | |
</application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Platform.OS === "android") { | |
const hasPermissions = await requestStoragePermission(); // Use await here | |
if (hasPermissions) { | |
exportData(); // Call the export data function if permissions are granted | |
} | |
} else { | |
exportData(); // Directly call the export data function for non-Android platforms | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PermissionsAndroid, Alert, Linking, Platform } from 'react-native'; | |
export const requestStoragePermission = async () => { | |
try { | |
let permissionGranted = false; | |
const androidVersion = parseInt(Platform.Version as string, 10); // Get the Android version as an integer | |
if (androidVersion >= 33) { | |
// Request permissions for Android 13 and above (API level 33+) | |
const requestedPermissions = [ | |
PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES, | |
PermissionsAndroid.PERMISSIONS.READ_MEDIA_VIDEO, | |
PermissionsAndroid.PERMISSIONS.READ_MEDIA_AUDIO, | |
]; | |
const results = await PermissionsAndroid.requestMultiple(requestedPermissions); | |
permissionGranted = Object.values(results).some(result => result === PermissionsAndroid.RESULTS.GRANTED); | |
} else if (androidVersion <= 32) { | |
// Request permissions for Android 11 to 13 (API level 30-32) | |
const requestedPermissions = [ | |
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, | |
PermissionsAndroid.PERMISSIONS.MANAGE_EXTERNAL_STORAGE, | |
]; | |
const results = await PermissionsAndroid.request( | |
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, | |
); | |
console.log(results); | |
permissionGranted = results === PermissionsAndroid.RESULTS.GRANTED; | |
} else { | |
openSettingsAlert(); // Open settings alert if the Android version is unsupported | |
} | |
if (permissionGranted) { | |
return true; // Return true if permissions are granted | |
} else { | |
openSettingsAlert(); // Open settings alert if permissions are denied | |
return false; // Return false if permissions are denied | |
} | |
} catch (err) { | |
console.warn(err); // Log any errors | |
return false; // Return false in case of error | |
} | |
}; | |
export const openSettingsAlert = () => { | |
Alert.alert('Required permission.', 'Storage permission required to download the data', [ | |
{ | |
text: 'Cancel', | |
onPress: () => console.log('Cancel Pressed'), // Log when Cancel is pressed | |
style: 'cancel', | |
}, | |
{ | |
text: 'Go To Settings', | |
onPress: () => openAppSettings() // Open app settings when "Go To Settings" is pressed | |
}, | |
]); | |
}; | |
const openAppSettings = () => { | |
if (Platform.OS === 'ios') { | |
Linking.openURL('app-settings:'); // Open iOS app settings | |
} else { | |
Linking.openSettings(); // Open Android app settings | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment