getTemporaryDirectory() 和 getApplicationSupportDirectory() 有什么区别?

What is the difference between getTemporaryDirectory() and getApplicationSupportDirectory()?

await getTemporaryDirectory();两者的主要区别是什么 await getApplicationSupportDirectory() 使用 Flutter Path 提供程序包。

根据文档:

getTemporaryDirectory :

Path to the temporary directory on the device that is not backed up and is suitable for storing caches of downloaded files.

Files in this directory may be cleared at any time. This does not return a new temporary directory. Instead, the caller is responsible for creating (and cleaning up) files or directories within this directory. This directory is scoped to the calling application.

On iOS, this uses the NSCachesDirectory API.

On Android, this uses the getCacheDir API on the context.

getApplicationSupportDirectory :

Path to a directory where the application may place application support files.

Use this for files you don’t want exposed to the user. Your app should not use this directory for user data files.

On iOS, this uses the NSApplicationSupportDirectory API. If this directory does not exist, it is created automatically.

On Android, this function uses the getFilesDir API on the context.

提到函数的底层调用是什么:

getTemporaryDirectory , getApplicationSupportDirectory

考虑Android的例子,两种方法返回的路径应该用于存储app-related数据。

但有一点不同,对于getTemporaryDirectory返回路径内的文件,系统会自动删除该目录下的文件,因为设备其他地方需要磁盘space。系统将始终根据 lastModifiedTime 首先删除较旧的文件。

这两种方法都不需要存储权限。