创建于Android的临时文件临时文件、创建于、Android

2023-09-12 01:29:35 作者:满目逢秋

什么是建立在Android的临时文件的最好方法是什么?

What's the best way to create a temporary file in Android?

能否File.createTempFile使用?该文档是非常模糊了。

Can File.createTempFile be used? The documentation is very vague about it.

在特定的,目前尚不清楚何时与 File.createTempFile 创建的临时文件被删除,如果有的话。

In particular, it's not clear when temporary files created with File.createTempFile are deleted, if ever.

推荐答案

这就是我通常做的:

File outputDir = context.getCacheDir(); // context being the Activity pointer
File outputFile = File.createTempFile("prefix", "extension", outputDir);

至于其删除,我不完全肯定无论是。因为我用这个在我实施的缓存,我手动删除旧的文件,直到缓存目录大小归结到我的preSET值。

As for their deletion, I am not complete sure either. Since I use this in my implementation of a cache, I manually delete the oldest files till the cache directory size comes down to my preset value.