0
import RNFetchBlob from 'rn-fetch-blob'


    var path = RNFetchBlob.fs.dirs.DocumentDir 
    RNFetchBlob
                    .config({ path: toFile })
                    .fetch('GET', fromUrl)
                    .then(res => {
                       
                    })
            });

Using the above code for downloading Files in the React native and working fine in the ios. But i am doing this in android and it is showing downloading but not found files in the directly. I want to save files in the (Android=>data=>com.appname) folder. But this code is not working to download it.

I tried DownloadDir that is working and saving files in the download folder. But i want to save it in the (Android=>data=>com.appname) folder. So not able to get that how it will work on it.

Rover
  • 661
  • 2
  • 18
  • 39

1 Answers1

2

This example saves files at below path under MyApp Folder

/data/user/0/com.filesystem/files/MyApp/

Its not showing the content via external file manager due to android latest privacy policy (I think from Android 10 +) for that external file manager app have to take extra permission.

You can use below file manager to view files inside data folder https://play.google.com/store/apps/details?id=com.alphainventor.filemanager

Android Vesion enter image description here

AndroidManifest.xml

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

   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
   <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

    <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|screenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
        </intent-filter>
      </activity>
    </application>
</manifest>

App.js

import React, { useEffect, useState } from 'react'
import { SafeAreaView, View, Text, TouchableOpacity, FlatList, Image, Dimensions } from 'react-native'
import RNFetchBlob from 'rn-fetch-blob'

const { height, width } = Dimensions.get('window')

export default function App() {

  const [files, setFiles] = useState([])

  useEffect(() => {
    handleGetFileList()
  }, [])


  async function handleGetFileList() {

    const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'MyApp'

    await RNFetchBlob.fs.isDir(path).then(isDir => {
      console.log('isDir', isDir)
      if (isDir == true) {
        RNFetchBlob.fs.lstat(path).then(filesList => {
          console.log('filesList', filesList)
          setFiles(filesList)
        })
          .catch(e => {
            console.log('Unable to get files list', e)
          })
      }
    })
      .catch(e => {
        console.log('Error isDir', e)
      })
  }

  function handleDownloadFile() {
    console.log('Hiii')
    const destinationPath = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'MyApp'
    const url = 'https://shotkit.com/wp-content/uploads/2021/06/cool-profile-pic-matheus-ferrero.jpeg'
    const fileName = Date.now()
    const fileExtention = url.split('.').pop();
    const fileFullName = fileName + '.' + fileExtention
    console.log('fileName', fileName)
    console.log('fileExtention', fileName)
    console.log('fileName', fileFullName)
    RNFetchBlob
      .config({ path: destinationPath + '/' + fileFullName, fileCache: true })
      .fetch('GET', url)
      .then((res) => {
        console.log('The file saved to ', res.path())
        handleGetFileList()
      })
  }


  function handleDeleteFiles() {
    const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'MyApp'
    RNFetchBlob.fs.unlink(path)
      .then(() => {
        setFiles([])
      })
      .catch((err) => { })
  }

  function renderItem({ item, index }) {
    return (
      <Image
        source={{ uri: 'file://' + item.path }}
        style={{ height: 150, width: width / 4, borderRadius: 10, borderWidth: 1, borderColor: 'black', margin: 10 }}
        resizeMode='cover'
      />


    )
  }


  return (
    <SafeAreaView style={{ flex: 1, alignItems: 'center', justifyContent: 'center', }}>


      <View style={{ flex: 4, alignItems: 'center', justifyContent: 'space-around', }}>

        <TouchableOpacity
          onPress={handleGetFileList}
          style={{ height: 45, width: 150, borderRadius: 10, borderWidth: 1, borderColor: 'black', alignItems: 'center', justifyContent: 'center', }}>
          <Text>
            Get the files
          </Text>
        </TouchableOpacity>

        <TouchableOpacity
          onPress={handleDownloadFile}
          style={{ height: 45, width: 150, borderRadius: 10, borderWidth: 1, borderColor: 'black', alignItems: 'center', justifyContent: 'center', }}>
          <Text>
            Download the files
          </Text>
        </TouchableOpacity>

        <TouchableOpacity
          onPress={handleDeleteFiles}
          style={{ height: 45, width: 150, borderRadius: 10, borderWidth: 1, borderColor: 'black', alignItems: 'center', justifyContent: 'center', }}>
          <Text>
            Delete all files
          </Text>
        </TouchableOpacity>

        {/* <View style={{ height: '100%', width: 10 }} /> */}


      </View>

      <View style={{ flex: 6 }}>
        <FlatList
          data={files}
          keyExtractor={(item, index) => String(index)}
          renderItem={renderItem}
          numColumns={3}
        />
      </View>


    </SafeAreaView>
  )
}

Preview

Hardik Desai
  • 1,089
  • 12
  • 20
  • 3-4 is working but 1-2 not working. As i need to store in com.appname folder. I am getting that it is showing me data transfer but not finding on this location. – Rover Mar 29 '22 at 12:59
  • const dirs = RNFetchBlob.fs.dirs This is not working on android as it is showing data download but files are not available in this folder. I also added permissions of external_storage. – Rover Mar 29 '22 at 12:59
  • Please check the permission in Android Manifest file – Hardik Desai Mar 29 '22 at 13:01
  • I have added permissions there for storage. Any other permission is required? – Rover Mar 29 '22 at 13:07
  • You are using andorid 11 ? – Hardik Desai Mar 29 '22 at 13:10
  • My android is 12 – Rover Mar 29 '22 at 13:14
  • Please try updated answer – Hardik Desai Mar 29 '22 at 13:26
  • Not working to store in app location – Rover Mar 30 '22 at 04:29
  • I create one example with Android 12 please try @Rover – Hardik Desai Mar 30 '22 at 07:29
  • files are downloading as these are visible in the app. But i am not able to browse these. Even i tried with the above file manager and i am getting the folders are empty. Have you checked the folders after downloading the images? – Rover Mar 30 '22 at 09:10
  • I am not getting that where are these files going as i also checked with hidden folders – Rover Mar 30 '22 at 09:11
  • I already mentioned in last updated answer please read carefully the files are there but you'cant see files directlly due to googles updated privacy policy You can do that programatically as I mentioned in answer or you can use exeternal file explorer software to view that which is https://play.google.com/store/apps/details?id=com.alphainventor.filemanager – Hardik Desai Mar 30 '22 at 10:21
  • I read it and Used the same app to view it. Still with this app not able to see it. – Rover Mar 30 '22 at 11:23
  • With above code you can 1 Able to dowload file at Android=>data=>com.appname => Done 2 View downloaded file programatically in our app => Done 3 Directly view the file via file manager app => Not possible from Android 10+ (You are running 12) Due to update privacy policy of google 4 View the file via 3rd party tool at Android=>data=>com.appname this path use below software https://play.google.com/store/apps/details?id=com.alphainventor.filemanager So now were the issue you are facing that I did't get it. – Hardik Desai Mar 30 '22 at 12:37