react-native-fs 使用

使用 api 非常簡單,可直接參閱 官方文檔,這里僅對(duì)其路徑常量做一個(gè)簡單解釋

android

MainBundlePath: undefined
DocumentDirectoryPath: "/data/user/0/com.project/files"
LibraryDirectoryPath: undefined
CachesDirectoryPath: "/data/user/0/com.project/cache"
TemporaryDirectoryPath: "/data/user/0/com.project/cache"
ExternalDirectoryPath: "/storage/emulated/0/Android/data/com.project/files"
ExternalCachesDirectoryPath: "/storage/emulated/0/Android/data/com.project/cache"
ExternalStorageDirectoryPath: "/storage/emulated/0"
PicturesDirectoryPath: "/storage/emulated/0/Pictures"
FileProtectionKeys: undefined

iOS

MainBundlePath: "/data/Containers/Bundle/Application/E57.../project.app"
DocumentDirectoryPath: "/data/Containers/Data/Application/F18.../Documents"
LibraryDirectoryPath: "/data/Containers/Data/Application/F18.../Library"
CachesDirectoryPath: "/data/Containers/Data/Application/F18.../Library/Caches"
TemporaryDirectoryPath: "/data/Containers/Data/Application/F18.../tmp"
ExternalDirectoryPath: null
ExternalCachesDirectoryPath: undefined
ExternalStorageDirectoryPath: null
PicturesDirectoryPath: undefined
FileProtectionKeys: undefined

MainBundlePath

打包 app 的絕對(duì)路徑,相當(dāng)于你可以直接讀取 app 內(nèi)部的一些資源文件,僅在 iOS 下可用,使用的時(shí)候要注意拼接路徑; android 沒這個(gè)東西,做雙平臺(tái)的話,應(yīng)避免使用這個(gè),也沒必要,把資源直接打包,也會(huì)增加安裝包體積

DocumentDirectoryPath

存放 app 使用過程中產(chǎn)生的用戶私人文件,可持久化保存,iTunes iCloud 備份/恢復(fù)包含此目錄,想必 android 應(yīng)該是同理的

LibraryDirectoryPath

存儲(chǔ)配合應(yīng)用程序的其他非個(gè)人文件的數(shù)據(jù),比如一些應(yīng)用程序的配置文件啥的,android 沒有這個(gè)目錄,也應(yīng)盡量避免使用,當(dāng)需要存儲(chǔ)此類數(shù)據(jù),建議放到 CachesDirectoryPath

CachesDirectoryPath

應(yīng)用程序數(shù)據(jù)緩存目錄,可持久化保存,但可能面臨被清除的風(fēng)險(xiǎn)(比如一些清理垃圾文件的操作),所以應(yīng)用程序如果使用該目錄應(yīng)該做兜底處理

TemporaryDirectoryPath

臨時(shí)文件目錄,這個(gè)純粹是臨時(shí)的,重啟手機(jī),或者文件過多,系統(tǒng)層面就直接清除這個(gè)目錄,都無需用戶去特意觸發(fā),用來存放用一下子就仍的文件。 Android 沒這個(gè)目錄,react-native-fs 默認(rèn)返回了 CachesDirectoryPath 的路徑

ExternalDirectoryPath
ExternalStorageDirectoryPath

sd卡目錄,僅在 Android 上生效,對(duì)應(yīng)著 DocumentDirectoryPath、CachesDirectoryPath,無需申請(qǐng)權(quán)限,屬于應(yīng)用專屬目錄

ExternalStorageDirectoryPath
PicturesDirectoryPath

看路徑應(yīng)該是 android 共享的 sd 卡目錄,需要申請(qǐng)權(quán)限才能使用,由于是 android only,且需要權(quán)限,也建議盡量不使用

FileProtectionKeys

沒看到項(xiàng)目中提到,暫且不理會(huì)

總結(jié)

  • 寫永久保存的個(gè)人文件,用 DocumentDirectoryPath 是最好的,雙平臺(tái)兼容。

  • 寫緩存文件用 CachesDirectoryPath 最好,也是雙平臺(tái)兼容。

  • 寫臨時(shí)文件,iOS 可直接用 TemporaryDirectoryPath, android 建議使用 CachesDirectoryPath ,用完之后手動(dòng)刪除,可將二者封裝為一個(gè)兼容的函數(shù)

  • Android SD 卡,除非是做文件管理相關(guān)的應(yīng)用,不建議使用相關(guān)路徑,畢竟現(xiàn)在的手機(jī)存儲(chǔ)空間都比較大,甚至不少手機(jī)都取消了 sd 卡

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,334評(píng)論 0 17
  • 我想一次性付費(fèi),閱讀所有 Tasker 中文用戶指南哦?OK,請(qǐng)?jiān)L問此鏈接 原創(chuàng)內(nèi)容,未經(jīng)本人授權(quán)不得轉(zhuǎn)載
    blackspot閱讀 1,513評(píng)論 0 1
  • 從今天開始,我要連續(xù)寫3篇從心理角度去了解孩子、走進(jìn)孩子的故事。 第1篇講的是:孩子畫畫作品背后隱藏的內(nèi)心世界。 ...
    頑顏瞳閱讀 4,708評(píng)論 1 7
  • 溜溜梅低年級(jí)“天天圖說”: 時(shí)間:2月6日 題目:奶奶笑了 小腦袋想一想:什么時(shí)間?什么地方?誰在做什么?她們說了...
    或亦馨也閱讀 513評(píng)論 0 0
  • 今天醒來之后,發(fā)現(xiàn)自己的心情很不好,是因?yàn)榻裉礻幪炷?,還是因?yàn)閴?mèng)里夢(mèng)見了什么。 不自覺的唱起了林俊杰翻唱的《我懷念...
    予你摯終vvvv閱讀 106評(píng)論 0 0

友情鏈接更多精彩內(nèi)容