Android
// /data/data/包名/files
context.getFilesDir();
// /data/data/包名/cache
context.getCacheDir(); //这两个文件夹下的内容会随着app卸载而删除
// /storage/emulated/0
Environment.getExternalStorageDirectory();
// /storage/emulated/0/DCIM, 另外还有MOVIE/MUSIC等很多种标准路径
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
// /storage/emulated/0/Android/data/packname/files
getExternalFilesDir(“”).getAbsolutePath()
// /storage/emulated/0/Android/data/packname/cache
getExternalCacheDir().getAbsolutePath()
// 遍历存储路径(有sd卡会额外打印)
File[] files;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
for(File file:files){
Log.e("main",file);
}
}
ReactNative
借助第三方库react-native-fs实现
yarn add react-native-fs //或者npm install react-native-fs --save
react-native link react-native-fs
使用
var RNFS = require('react-native-fs');
// undefined
console.log('MainBundlePath',RNFS.MainBundlePath)
// /data/data/com.rn_test_demo/cache
console.log('CachesDirectoryPath',RNFS.CachesDirectoryPath)
// /data/data/com.rn_test_demo/files
console.log('DocumentDirectoryPath',RNFS.DocumentDirectoryPath)
// /data/data/com.rn_test_demo/cache
console.log('TemporaryDirectoryPath',RNFS.TemporaryDirectoryPath)
// undefined
console.log('LibraryDirectoryPath',RNFS.LibraryDirectoryPath)
// /storage/emulated/0/Android/data/com.rn_test_demo/files
console.log('ExternalDirectoryPath',RNFS.ExternalDirectoryPath)
// /storage/emulated/0
console.log('ExternalStorageDirectoryPath',RNFS.ExternalStorageDirectoryPath)