Yet another software engineer

My thoughts are my own

React native file system new file is not visible in computer via USB/MTP

Posted at — Dec 9, 2019

Problem summary

I was working on a react native project where I had to create a .csv file and store it in external/internal storage of the device. So that the user can transfer the file via USB and do something with it. My target platform was Android. Everything was okay until I tried to access my generate .csv file via USB. The file was in the device but I couldn’t see it from my computer. Very strange problem. I can view the file from my device’s file explorer but can not access it from computer.

After a few minutes of googling I have found that this is a very known problem of MTP protocol. I had to restart the device to re-scan the file system and access it from computer.

Solution

Restarting is not a solution. So, the solution was to manually run the MediaScanner for scanning of the file system. The file system module for react native has a function named scanFile which is a JavaScript binding.

Example usage

const RNFS = require('react-native-fs');
RNFS.touch(RNFS.DocumentDirectoryPath + '/test.txt')
.then(async path => {
    await RNFS.scanFile(path); // Scanning the newly create file
});