Helper utils for common problems.
Utils:
getPathFromURI
: Convert Uri to real device path for Android- Known paths for Android:
PicturesDirectoryPath
: Internal pictures directoryDownloadsDirectoryPath
: Internal downloads directoryDCIMDirectoryPath
: Internal DCIM directoryCachesDirectoryPath
: Application cache directoryFilesDirectoryPath
: Application files directory
npm i react-native-file-utils --save
react-native link react-native-file-utils
OR
android/settings.gradle
...
include ':react-native-file-utils'
project(':react-native-file-utils').projectDir = new File(settingsDir, '../node_modules/react-native-file-utils/android')
android/app/build.gradle
...
dependencies {
...
compile project(':react-native-file-utils')
}
- register module (in MainActivity.java)
import com.anumang.rnfileutils.RNFUPackage; // <------- add package
public class MainActivity extends ReactActivity {
// ...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(), // <---- add comma
new RNFUPackage() // <---------- add package
);
}
// require the module
var RNFU = require('react-native-file-utils');
RNFU.getPathFromURI(uriString).then(filePath =>
console.log(filePath)
)
- Required: react-native-fs https://github.com/johanneslumpe/react-native-fs
RNFU.getPathFromURI(imageUri).then(path =>
RNFS.readFile(path, 'base64').then(imageBase64 =>
console.log(imageBase64)
)
)
- Required: react-native-fs https://github.com/johanneslumpe/react-native-fs
let filePath = RNFU.DCIMDirectoryPath + fileName;
RNFU.writeFile(filePath, base64Content, 'base64').then(path =>
RNFS.readFile(path, 'base64').then(imageBase64 =>
console.log(imageBase64)
)
)