反应原生 fs 库不写入文件 [英] react native fs library not writing files

查看:36
本文介绍了反应原生 fs 库不写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 react-native-fs 创建文件时,回调成功到达,但文件不在我的 android/data/com.myapp/files/test.txt 库中.

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.

logcat 中没有错误,所以我想知道为什么我的带有简单 App.js 的代码不起作用.

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,
  },
});

我需要将 log.txt 写入应用程序文件夹,但无法创建新文件.我会很感激每一个帮助或建议.

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 myappcd myapp纱线添加 react-native-fs反应本机链接反应本机-fsreact-native run-android

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

推荐答案

您可以尝试将 DocumentDirectoryPath 替换为 ExternalStorageDirectoryPath.

Can you try replacing DocumentDirectoryPath with ExternalStorageDirectoryPath.

这篇关于反应原生 fs 库不写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆