0

I am creating a app and need external storage permission. I wrote the above code but it's not opening the prompt where we can allow or deny the permission. also I have added user permission in my manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
    </application>
</manifest>

This is mt AndroidManifest.xml file.

All file code

import {View, Text, PermissionsAndroid, Alert} from 'react-native';
import React from 'react';
import {TouchableOpacity} from 'react-native-gesture-handler';
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import {useNavigation} from '@react-navigation/native';
import RNFetchBlob from 'rn-fetch-blob';
const PDFComponent = () => {
  const link = 'https://www.africau.edu/images/default/sample.pdf';
  const navigation = useNavigation();
  const actualDownload = () => {
    const {config, fs} = RNFetchBlob;
    const date = new Date();
    const fileDir = fs.dirs.DownloadDir;
    config({
      fileCache: true,
      addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        description: "download file",
        path: fileDir + '/download_' + Math.floor(date.getDate() + date.getSeconds() / 2) + '.pdf'
      }
    })
      .fetch('GET', 'http://www.africau.edu/images/default/sample.pdf', {})
      .then(res => {
        console.log('The file saved to ', res.path());
      })
      .catch(e => {
        console.log(e);
      });
  };

  const downloadFile = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        {
          title: 'Spardha App Storage Permission',
          message: 'Spardha App needs access to Storage',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        actualDownload();
      } else {
        Alert.alert(
          'Permission Denied!',
          'You need to give storage permission to download the file',
        );
      }
    } catch (err) {
      console.warn(err);
    }
  };
  return (
    <View style={{padding: 20}}>
      <TouchableOpacity
        onPress={() => navigation.navigate('PDFScreen', {pdf: {link}})}
        style={{
          shadowColor: 'black',
          shadowOffset: {height: 2, width: 2},
          shadowOpacity: 1,
          elevation: 4,
          backgroundColor: 'white',
          borderRadius: 10,
        }}>
        <View
          style={{
            flexDirection: 'row',
            padding: 20,
            alignItems: 'center',
            justifyContent: 'space-around',
          }}>
          <View>
            <Text>
              <FontAwesome5 name="file-pdf" size={25} color="black" />
            </Text>
          </View>
          <View style={{}}>
            <Text style={{color: 'black', fontSize: 15}}>PDF Name</Text>
          </View>
          <TouchableOpacity onPress={downloadFile}>
            <MaterialCommunityIcons name="download" size={25} color="black" />
          </TouchableOpacity>
        </View>
      </TouchableOpacity>
    </View>
  );
};

export default PDFComponent;

This is my code in that particular file. what should i do now?

0 Answers0