Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native fs library not writing files

when i use react-native-fs to create file, success in callback arrives but the file isn't in my android/data/com.myapp/files/test.txt library.

No errors in logcat so i'm wondering why my code with simple App.js not working.

    /**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  ToastAndroid
} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});
var RNFS = require('react-native-fs');
 // create a path you want to write to
 var path = RNFS.DocumentDirectoryPath + '/test.txt';
export default class App extends Component {
  onSave = () => {
    RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
.then((success) => {
 console.log('FILE WRITTEN!');
})
.catch((err) => {
 console.log(err.message);
});
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <TouchableOpacity onPress={() => this.onSave()}>
        <Text style={styles.instructions}>
          To get started, edit App.js
        </Text>
        </TouchableOpacity>
        <Text style={styles.instructions}>
          {instructions}
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

I need to write log.txt into the app folder but impossible to create new file. I would appreciate every help or suggestion.

react-native init myapp cd myapp yarn add react-native-fs react-native link react-native-fs react-native run-android

like image 392
Vojtěch Prchal Avatar asked Mar 28 '18 07:03

Vojtěch Prchal


1 Answers

Can you try replacing DocumentDirectoryPath with ExternalStorageDirectoryPath.

like image 159
HSBP Avatar answered Nov 18 '22 09:11

HSBP