在 Android 中创建临时文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3425906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating temporary files in Android
提问by hpique
What's the best way to create a temporary file in Android?
在 Android 中创建临时文件的最佳方法是什么?
Can File.createTempFilebe 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.createTempFile
删除创建的临时文件,如果有的话。
回答by sparkymat
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.
至于他们的删除,我也不完全确定。因为我在缓存的实现中使用它,所以我手动删除最旧的文件,直到缓存目录大小降到我的预设值。
回答by mibollma
Best practices on internaland externaltemporary files:
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.
如果您想缓存一些数据,而不是永久存储它,您应该使用
getCacheDir()
打开一个 File 来表示您的应用程序应该保存临时缓存文件的内部目录。当设备内部存储空间不足时,Android 可能会删除这些缓存文件以恢复空间。但是,您不应依赖系统为您清理这些文件。您应该始终自己维护缓存文件,并保持在合理的空间消耗限制内,例如 1MB。当用户卸载您的应用程序时,这些文件将被删除。
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 callingContextCompat.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.
要打开一个代表您应该保存缓存文件的外部存储目录的文件,请调用
getExternalCacheDir()
. 如果用户卸载您的应用程序,这些文件将被自动删除。与
ContextCompat.getExternalFilesDirs()
上面提到的类似,您还可以通过调用 来访问辅助外部存储(如果可用)上的缓存目录ContextCompat.getExternalCacheDirs()
。提示:要保留文件空间并保持应用程序的性能,请务必谨慎管理缓存文件并删除在应用程序的整个生命周期中不再需要的文件。
回答by AdrenalineJunky
For temporary internal files their are 2 options
对于临时内部文件,它们有 2 个选项
1.
1.
File file;
file = File.createTempFile(filename, null, this.getCacheDir());
2.
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.
这两个选项都在应用程序缓存目录中添加文件,因此可以根据需要清除以腾出空间,但选项 1 将在文件名的末尾添加一个随机数以保持文件的唯一性。它还将添加.tmp
默认的文件扩展名,但可以通过使用第二个参数将其设置为任何内容。随机数的使用意味着尽管指定了文件名,但它不会与随后缀/文件扩展名(.tmp
默认情况下)一起添加的数字保持不变,例如,您将文件名指定internal_file
为internal_file1456345.tmp
. 您可以指定扩展名,但不能指定添加的号码。但是,您可以通过以下方式找到它生成的文件名file.getName();
,但您需要将它存储在某个地方,以便您可以随时使用它,例如删除或读取文件。因此,出于这个原因,我更喜欢第二个选项,因为您指定的文件名是创建的文件名。
回答by Macarse
You can use the cache dir using context.getCacheDir().
您可以使用context.getCacheDir()使用缓存目录。
File temp=File.createTempFile("prefix","suffix",context.getCacheDir());
回答by kotucz
You can use the File.deleteOnExit()
method
您可以使用该File.deleteOnExit()
方法
https://developer.android.com/reference/java/io/File.html#deleteOnExit()
https://developer.android.com/reference/java/io/File.html#deleteOnExit()
It is referenced here https://developer.android.com/reference/java/io/File.html#createTempFile(java.lang.String, java.lang.String, java.io.File)
这里引用https://developer.android.com/reference/java/io/File.html#createTempFile(java.lang.String, java.lang.String, java.io.File)
回答by Shivam Dawar
Do it in simple. According to documentation https://developer.android.com/training/data-storage/files
做起来很简单。根据文档 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()