react-native-fs
RN对文件的操作
1:install:
npm install react-native-fs --save
2:Android:
android/setting.gradle或者项目根目录找到setting.gradle
include ':app' include ':react-native-fs' project(':react-native-fs').projectDir = new File(settingsDir, './node_modules/react-native-fs/android')
android/app/build.gradle或者项目根目录找到app/build.gradle
... dependencies { ... compile project(':react-native-fs') }
Register module (in MainApplication.java)或者在根目录找到app/src/com/android/browser/page/HomePage.java
import com.rnfs.RNFSPackage; // <--- import
public class MainApplication extends Application implements ReactApplication {
......
@Override
protected List
3:index.android.js:
import {AppRegistry} from 'react-native'; import App from './App'; AppRegistry.registerComponent('LeecoBrowser', () => App);
4:App.js:
import React, { Component } from 'react'; import { View, Text, } from 'react-native'; import RNFS from 'react-native-fs'; //文件模块 class App extends Component { constructor(props) { super(props); } componentWillMount() { console.log("RNFS",RNFS) } render() { return (
5:结果:
'RNFS', { mkdir: [Function: mkdir], moveFile: [Function: moveFile], copyFile: [Function: copyFile], pathForBundle: [Function: pathForBundle], getFSInfo: [Function: getFSInfo], unlink: [Function: unlink], exists: [Function: exists], stopDownload: [Function: stopDownload], stopUpload: [Function: stopUpload], readDir: [Function: readDir], readdir: [Function: readdir], stat: [Function: stat], readFile: [Function: readFile], writeFile: [Function: writeFile], appendFile: [Function: appendFile], downloadFile: [Function: downloadFile], uploadFiles: [Function: uploadFiles], MainBundlePath: undefined, CachesDirectoryPath: '/data/user/0/com.android.browser/cache', DocumentDirectoryPath: '/data/user/0/com.android.browser/files', ExternalDirectoryPath: '/storage/emulated/0/Android/data/com.android.browser/files', ExternalStorageDirectoryPath: '/storage/emulated/0', TemporaryDirectoryPath: null, LibraryDirectoryPath: undefined, PicturesDirectoryPath: '/storage/emulated/0/Pictures' }
6:分析
RNFS.PicturesDirectoryPath : 截图的目录 RNFS.CachesDirectoryPath : 缓存文件的目录 RNFS.DocumentDirectoryPath : 文件存储的目录