Search code examples
androidreactjsreact-nativeandroid-studioexport-to-pdf

Download File in react native


even though the permission is given my code throws permission required error i want to save the image like when i write const granted = getPermissionAndroid(); it saves image when the permission is not given and when i write const granted = await getPermissionAndroid(); even though the permission is given it says Permission required

const onClickExportImage = useCallback(async () => {
    try {
        const uri = await viewShotRef.current.capture();
        if (Platform.OS === 'android') {
            const granted = await getPermissionAndroid(); 
        if (!granted) {
            return;
        }
    }
    const newURI = await CameraRoll.save(uri, {
        type: 'photo',
        album: 'Lukim Gather',
    });
    Toast.show(_('Saved image in gallery!'));
    setTimeout(() => {
        Linking.openURL(newURI);
    }, 1000);
    setIsOpenExport(false);
} catch (error) {
    console.log(error);
}

}, [getPermissionAndroid]);

const getPermissionAndroid = useCallback(async () => {
    try {
        const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
            {
                title: _('Image export permission'),
                message: _('Your permission is required to save image'),
                buttonNegative: _('Cancel'),
                buttonPositive: _('OK'),
            },
        );
        console.log('Permission result:', granted);
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            return true;
        } else {
            setIsOpenExport(false);
            Toast.error(_('Permission denied!'));
            return false;
        }
    } catch (err) {
        console.log('Error' + err);
        return false;
    }
}, []);

Solution

  • Solution:- Since WRITE_EXTERNAL_STORAGE is deprecated for Android 11 (API level 30) and above, you need to use Media Store API or Scoped Storage.

        const getPermissionAndroid = useCallback(async () => {
        try {
        if (Platform.Version >= 33) {
        // Request permission for managing external storage (Android 11+)
        const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES, // For accessing media files
        {
         title: 'File Download Permission',
         message: 'Your permission is required to save Files to your device',
         buttonNegative: 'Cancel',
         buttonPositive: 'OK',
         },
         );
    
         if (granted === PermissionsAndroid.RESULTS.GRANTED) {
         return true;
         } else {
         setIsOpenExport(false); Toast.error(('Permission required'));
         return false;
         }
         }
         else if (granted === PermissionsAndroid.RESULTS.GRANTED) {
         return true;
         }
    
        else {
        // For API levels < 33
        const granted = await PermissionsAndroid.request(
       PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
       {
         title: 'File Download Permission',
         message: 'Your permission is required to save Files to your device',
         buttonNegative: 'Cancel',
         buttonPositive: 'OK',
         },
         );
         if (granted === PermissionsAndroid.RESULTS.GRANTED) {
         return true;
         } else {
         setIsOpenExport(false); Toast.error(('Permission required'));
         return false;
         }
         }
    
         } catch (err) { console.log('Error' + err); return false; }
         }, [])