安卓:我应该在哪里保存临时文件?临时文件

由网友(一步踏尽一树白)分享简介:我的应用程序允许用户创建和修改文件。我希望他们能够将文件发送作为电子邮件附件。所以,我需要首先创建并写入到一个临时文件,然后我附加到电子邮件。不幸的是,基于对以下问题的唯一的回应,似乎没有知道的电子邮件应用程序与临时文件做了很好的途径。My application allows users to create and...

我的应用程序允许用户创建和修改文件。我希望他们能够将文件发送作为电子邮件附件。所以,我需要首先创建并写入到一个临时文件,然后我附加到电子邮件。不幸的是,基于对以下问题的唯一的回应,似乎没有知道的电子邮件应用程序与临时文件做了很好的途径。

My application allows users to create and modify files. I would like them to be able to send a file as an email attachment. So, I need to first create and write to a temporary file, which I then attach to the email. Unfortunately, based on the lone response to the below question, it seems there is no good way to know that the email application is done with the temporary file.

Android:问题附件从我的应用程序发送电子邮件。

由于我无法接收的电子邮件与文件进行任何通知,我的规则删除临时文件pretty的坏。他们像检查临时文件的onPause和的onCreate; 5分多钟的老删除任何东西。

Because I can't receive any notification that the email is done with the file, my rules for deleting temporary files are pretty bad. They are something like "check for temp files onPause and onCreate; delete anything over 5 minutes old".

由于我的规则是如此的丑陋,我特别担心我应该在哪里写的文件。我不能把它们写入内部缓存目录,因为它们的大小可大大超过1MB。它是合理的,创建一个文件夹设备的SD卡:/ SD卡/ myapp_tmp?这究竟是什么情况,常见的做法?

Because my rules are so ugly, I'm particularly concerned where I should write the files. I can not write them to the internal cache directory because their size can greatly exceed 1mb. Is it reasonable to create a folder devices' sdcard: "/sdcard/myapp_tmp"? What is the common practice for this situation?

推荐答案

您可以使用右键的getExternalCacheDir()方法得到一个文件引用,您可以存储在SD卡上的文件。当然,你必须做平常检查,以确保外部存储设备安装和可写的,像往常一样,不过这可能是存储类型的临时文件的最好的地方。你可能想要做的一件事是刚刚设置的空间,你可以在缓存目录中使用的最高金额,然后,你需要编写一个新的临时文件,如果该文件超过了最大空间的任何时间,然后开始删除临时文件,从最古老的,直到有足够的空间。

You can use Context's getExternalCacheDir() method to get a File reference where you can store files on an SD card. Of course, you'll have to do the usual checks to make sure the external storage is mounted and writable, as usual, but this is probably the best place to store that type of temporary file. One thing you might want to do is just set a maximum amount of space that you can use in the cache directory, and then, any time you need to write a new temporary file, if that file exceeds the maximum space, then start deleting the temp files, starting with the oldest, until there is sufficient space.

编辑:或者,也许像这样的工作:

Alternatively, maybe something like this would work:

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    File externalRoot = Environment.getExternalStorageDirectory();
    File tempDir = new File(externalRoot, ".myAppTemp");
}

prepending了。应该使隐藏文件夹,我相当肯定。

Prepending the "." should make the folder hidden, I'm fairly sure.

阅读全文

相关推荐

最新文章