app中做一个备份功能,这时用到了react-native-fs,找了文档中readme的例子:
// require the module
var RNFS = require('react-native-fs');
// create a path you want to write to
var path = RNFS.DocumentDirectoryPath + '/test.txt';
// write the file
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
.then((success) => {
console.log('FILE WRITTEN!');
})
.catch((err) => {
console.log(err.message);
});
发现无论如何在存储卡中无法得到想要保存的文件。
搜索了一下issue,在这#273中找到了答案:
The RNFS object is display those paths:
CachesDirectoryPath:"/data/user/0/com.xxx.android.react.xxx/cache"
DocumentDirectoryPath:"/data/user/0/com.xxx.android.react.xxx/files"
ExternalDirectoryPath:"/storage/emulated/0/Android/data/com.xxx.android.react.xxx/files"
ExternalStorageDirectoryPath:"/storage/emulated/0"
LibraryDirectoryPath:undefined
MainBundlePath:undefined
PicturesDirectoryPath:"/storage/emulated/0/Pictures"
试了试,可行。看起来DocumentDirectoryPath
的数据似乎是只有root机器之后才能看到,而ExternalDirectoryPath
才是挂载的外置存储,可被用户随意查看。
update: 2019-08-09
坑又来了,前几天将半年没更新的项目升级到react-native 0.60.0
:
"react": "^16.8.6",
"react-native": "^0.60.0",
"react-native-fs": "^2.13.3",
#711又开始不能编译了,虽然改一下源码能稍微过关,但是总不能每次都改一下吧,换了多个开发机器同步更新,lock文件又会提示签名问题。
在issue中贴了一下log,不过看起来短时间内作者不打算合并了,所以说react-native
途径的并不能解救Java
布局患者。
update: 2021-04-11
关注了一下,发现issue依然未关~_~ 不过java又忘了差不多了,xml布局也能让人抓狂。语言选择苦难症…