在 Android 中创建临时文件

发布于 2024-09-13 09:05:24 字数 294 浏览 5 评论 0原文

在 Android 中创建临时文件的最佳方法是什么?

可以 文件。 createTempFile 可以使用吗?文档对此非常模糊。

特别是,尚不清楚使用 File.createTempFile 创建的临时文件何时会被删除(如果有的话)。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柠檬色的秋千 2024-09-20 09:05:24

这是我通常做的事情:

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

至于删除它们,我也不太确定。由于我在缓存的实现中使用了它,因此我手动删除最旧的文件,直到缓存目录大小降至我的预设值。

This is what I typically do:

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

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.

强辩 2024-09-20 09:05:24

内部外部临时文件:

内部缓存

如果您想缓存一些数据,而不是持久存储它,
您应该使用 getCacheDir() 打开一个代表
应用程序应保存临时缓存的内部目录
文件。

当设备内部存储空间不足时,Android 可能会删除
这些缓存文件可以回收空间。但是,您不应该依赖
系统会为您清理这些文件。你应该时刻保持
自己缓存文件并保持在合理的空间限制内
消耗,例如 1MB。当用户卸载您的应用程序时,
这些文件已被删除。

外部缓存

打开一个代表外部存储目录的文件,其中
您应该保存缓存文件,调用getExternalCacheDir()。如果用户
卸载您的应用程序,这些文件将自动
已删除。

与上面提到的ContextCompat.getExternalFilesDirs()类似,
您还可以访问辅助外部存储上的缓存目录
(如果可用)通过调用 ContextCompat.getExternalCacheDirs()。

<块引用>

提示:为了保留文件空间并保持应用的性能,请
重要的是您要仔细管理缓存文件并删除这些文件
在您的应用的整个生命周期中不再需要它们。

Best practices on internal and external temporary files:

Internal Cache

If you'd like to cache some data, rather than store it persistently,
you should use getCacheDir() to open a File that represents the
internal directory where your application should save temporary cache
files.

When the device is low on internal storage space, Android may delete
these cache files to recover space. However, you should not rely on
the system to clean up these files for you. You should always maintain
the cache files yourself and stay within a reasonable limit of space
consumed, such as 1MB. When the user uninstalls your application,
these files are removed.

External Cache

To open a File that represents the external storage directory where
you should save cache files, call getExternalCacheDir(). If the user
uninstalls your application, these files will be automatically
deleted.

Similar to ContextCompat.getExternalFilesDirs(), mentioned above,
you can also access a cache directory on a secondary external storage
(if available) by calling ContextCompat.getExternalCacheDirs().

Tip: To preserve file space and maintain your app's performance, it's
important that you carefully manage your cache files and remove those
that aren't needed anymore throughout your app's lifecycle.

梨涡少年 2024-09-20 09:05:24

对于临时内部文件,有 2 个选项

1.

File file; 
file = File.createTempFile(filename, null, this.getCacheDir());

2。

File file
file = new File(this.getCacheDir(), filename);

这两个选项都会在应用程序缓存目录中添加文件,因此可以根据需要清除以腾出空间,但选项 1 会在文件名末尾添加一个随机数以保持文件的唯一性。它还会添加一个文件扩展名,默认为 .tmp,但可以通过使用第二个参数将其设置为任何内容。使用随机数意味着尽管指定了文件名,但它不会保持与后缀/文件扩展名一起添加的数字相同(默认为 .tmp),例如,您将文件名指定为 < code>internal_file 并显示为 internal_file1456345.tmp。虽然您可以指定分机号,但无法指定添加的号码。不过,您可以通过 file.getName(); 找到它生成的文件名,但您需要将其存储在某个地方,以便您可以在需要时使用它,例如删除或读取文件。因此,出于这个原因,我更喜欢第二个选项,因为您指定的文件名是创建的文件名。

For temporary internal files their are 2 options

1.

File file; 
file = File.createTempFile(filename, null, this.getCacheDir());

2.

File file
file = new File(this.getCacheDir(), filename);

Both options adds files in the applications cache directory and thus can be cleared to make space as required but option 1 will add a random number on the end of the filename to keep files unique. It will also add a file extension which is .tmp by default, but it can be set to anything via the use of the 2nd parameter. The use of the random number means despite specifying a filename it doesn't stay the same as the number is added along with the suffix/file extension (.tmp by default) e.g you specify your filename as internal_file and comes out as internal_file1456345.tmp. Whereas you can specify the extension you can't specify the number that is added. You can however find the filename it generates via file.getName();, but you would need to store it somewhere so you can use it whenever you wanted for example to delete or read the file. Therefore for this reason I prefer the 2nd option as the filename you specify is the filename that is created.

ㄟ。诗瑗 2024-09-20 09:05:24

您可以使用 context.getCacheDir() 来使用缓存目录。

File temp=File.createTempFile("prefix","suffix",context.getCacheDir());

You can use the cache dir using context.getCacheDir().

File temp=File.createTempFile("prefix","suffix",context.getCacheDir());
笑着哭最痛 2024-09-20 09:05:24

简单点做吧。根据文件
https://developer.android.com/training/data-storage/files

String imageName = "IMG_" + String.valueOf(System.currentTimeMillis()) +".jpg";
        picFile = new File(ProfileActivity.this.getCacheDir(),imageName);

使用后删除

picFile.delete()

Do it in simple. According to documentation
https://developer.android.com/training/data-storage/files

String imageName = "IMG_" + String.valueOf(System.currentTimeMillis()) +".jpg";
        picFile = new File(ProfileActivity.this.getCacheDir(),imageName);

and delete it after usage

picFile.delete()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文