Save files in android/media/com.package

506 views Asked by At

I'm trying to download and save files in the file system like how WhatsApp does it.

WhatsApp stores files inside android/media/com.whatsapp/.

I was able to download and save the file inside /data/user/0/com.packagename/files using this library. I tried DocumentDir and PictureDir.

let dirs = ReactNativeBlobUtil.fs.dirs
ReactNativeBlobUtil
        .config({
            // response data will be saved to this path if it has access right.
            path: dirs.DocumentDir + '/path-to-file.anything'
        })
        .fetch('GET', 'http://www.example.com/file/example.zip', {
            //some headers ..
        })
        .then((res) => {
            // the path should be dirs.DocumentDir + 'path-to-file.anything'
            console.log('The file saved to ', res.path())
        })

Is there a way I can save them inside media folder instead of data?

I checked mediastore and other areas, but couldn't find a decent answer. Look into react-native-fs but couldn't find anything useful

Find more details here: https://github.com/RonRadtke/react-native-blob-util/issues/305

1

There are 1 answers

3
Shivo'ham On

Step 1 Create a package named folder inside the Android/media folder

Example:

const folderPath = "/storage/emulated/0/android/media/" + "com.example"
RNFS.mkdir(folderPath).catch((error) => { console.log(error) })
 

Step 2 Save the file to a specific path

Example:

const source = "dummy.pdf";
    const fileName = 'dummy.pdf';
    const storagePath = "/storage/emulated/0/android/media/" 
    const packageName = "com.example/"
    const folderPath =  "files/"+ fileName
    RNFetchBlob.config({
      fileCache: true,
      appendExt: 'pdf',
      path: (storagePath + packageName + folderPath),
    })
      .fetch('GET', source)
      .then((res) => {
        if (Platform.OS === 'ios') {
          const filePath = res.path();
          let options = {
            type: 'application/pdf',
            url: filePath,
            saveToFiles: true,
          };
          Share.open(options)
            .then((resp) => console.log(resp))
            .catch((err) => console.log(err));
        }
      })
      .catch((err) => console.log(err));

I used react native share for ios file downloading