Creating Temporary Files in Android

Creating temporary files in Android

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.

How to create a temporary file in android

You're trying to execute the command "logcat -d tempfilename" with no parameters, instead of the command "logcat" with parameters "-d" and "tempfilename". The OS is looking for an executable called "logcat -d tempfilename" instead of "logcat".

See this question for a code snippet that works (question itself, not the answers).

What is the best way to create temporary files on Android?

I finally figured out what was hapenning...

Question 1 : In fact, the file that was created was meant to be used by the system application to retrieve and crop an image from the user's gallery. So my app didn't have to own the WRITE_EXTERNAL_STORAGE permission, as the file was written by the system itself.

Question 2 : I'm not sure. I guess that the Android do not allow to create a temporary file where I was trying to (as it's not the right place for it)

File.createTempFile() VS new File()

Sounds like your app is creating files, so you need to guarantee unique filenames. You could keep some kind of counter within your app (saved to preferences or DB) and use that. Then you could create shorter/more controlled names, and control uniqueness yourself. Or you can use createTempFile(), which will guarantee you get a unique filename (but you only get partial control of the filename). Sounds like you prefer createTempFile(), so there's no reason not to continue using it if you are happy with the filenames it generates. There's no down side other than not having full control over the filename format.

Android How to use and create temporary folder

this code is to create folder:

File direct = new File(Environment.getExternalStorageDirectory() + "/New Folder");

if(!direct.exists())
{
(direct.mkdir()) //directory is created;

}

try it may help you



Related Topics



Leave a reply



Submit