Skip to content Skip to sidebar Skip to footer

How To Create A File And Store It Inside Download Folder Android?

I am using rn-fetch-blob https://github.com/joltup/rn-fetch-blob I am creating new file and storing it inside downloads folder. Note: I am using emulator. Now when I try to create

Solution 1:

For allowing acccess to other dirs in rn-fetch-blob if android version is above 6 then you need to ask for runtime permission. Link: https://facebook.github.io/react-native/docs/permissionsandroid

Modified code:

import {PermissionsAndroid} from'react-native';

    saveFile = async () => {

            try {
                const granted = awaitPermissionsAndroid.request(
                    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
                );
                if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                    console.log("Permission granted");

                    const fs = RNFetchBlob.fs;
                    // const base64 = RNFetchBlob.base64;const dirs = RNFetchBlob.fs.dirs;
                    console.log(dirs.DownloadDir);

                    constNEW_FILE_PATH = dirs.DownloadDir + '/test.txt';
                    fs.createFile(NEW_FILE_PATH, 'foo', 'utf8');

                } else {
                    console.log('Permission denied');
                }
                } catch (err) {
                    console.warn(err);
                }

        }

Additional links for rn-fetch-blob:

1) https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#dirs

2) https://github.com/joltup/rn-fetch-blob

Solution 2:

you need <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If you are using android 6 version or higher you need to ask for runtime permission

Post a Comment for "How To Create A File And Store It Inside Download Folder Android?"