2017-08-05 17 views
0

react-native-fsを使用してローカルファイルシステムからテキストファイルを読み取ろうとしています。ローカルファイルを読み込む

しかし、それはファイルが存在しないことを伝え続けます(私はそれをダブルチェックし、実行ウィンドウにコピーしようとしました)。

'use strict'; 

import React, { Component } from 'react'; 
//import FileSystem from 'react-native-filesystem'; 

import RNFS from 'react-native-fs'; 

import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    NetInfo 
} from 'react-native'; 


export default class FactsApiFetcher extends Component 
{ 
    constructor(props) 
    {  
    super(props); 
    this.state = 
    { 
     fact: 'Loading...' 
    } 
    } 

    componentWillMount() 
    { 
    this._getRandomFact(); 
    } 

    _getRandomFact() 
    { 
    const fileContents = RNFS.read("D:\\AllRand\\facts-api-handler\\test.txt") //FileSystem.readFile("D:\\AllRand\\allfacts\\test.txt");  

    this.setState({ 
     fact: fileContents 
    }) 
    } 

    render() 
    {  
    return <Text> { this.state.fact } </Text>; 
    } 
} 


AppRegistry.registerComponent('FactsApiFetcher',() => FactsApiFetcher); 
+0

設定をしましたか? https://github.com/itinance/react-native-fs#usage-androidにあり、そこにも使用例があります。 –

+1

ドキュメントhttps://github.com/itinance/react-native-fs#readfilepath-string-length--0-position--0-encodingoroptions-any-promisestringから、 'read(を使用すると約束を返す必要があります) ' –

答えて

3

ファイルをアプリケーションにバンドルするか、シミュレータまたはデバイス内に手動で配置する必要があります。アプリケーションを実行すると、それは自分のファイルシステムだけを探索することができ、Windows FSを解明することはできません。ファイルにアクセスするには、この定数https://github.com/itinance/react-native-fs#constantsを使用します。

関連する問題