Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Well, this is not exactly a question, as I'm not really "stuck" on my code, but I've found some strange behavior for Android API regarding accessing the external storage and the File.createTempFile() method, and I'd like to understand what's happening...

Please note that my manifest does not include the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">.


Part 1 :

I have the following code which does work as a charm :

File tempFile = new File(Environment.getExternalStorageDirectory(),
                      "my_temp_file.png");

it creates a temporary file for me, and I can write datas in it without any trouble.

Question 1 : Why does it work, as I'm not supposed to have writing rights on my SDCard ?


Part 2 :

I've tried to change my code to use the createTempFile which is the official method to create temporary file. So I've tried :

File tempFile = File.createTempFile("my_temp", "png", 
                      Environment.getExternalStorageDirectory());

and added the WRITE_EXTERNAL_STORAGE in my manifest.xml. Guess what ? This does not work, and I get a java.io.IOException :

09-07 14:07:29.061: E/_CLOG(19982): java.io.IOException: Permission denied
09-07 14:07:29.061: E/_CLOG(19982):     at java.io.File.createNewFileImpl(Native Method)
09-07 14:07:29.061: E/_CLOG(19982):     at java.io.File.createNewFile(File.java:1257)
09-07 14:07:29.061: E/_CLOG(19982):     at java.io.File.createTempFile(File.java:1322)
09-07 14:07:29.061: E/_CLOG(19982):     at com.(...).onClick(ProfileImageUpdater.java:58)

Question 2 : Why this doesn't work, whereas imho it should ?

like image 252
Orabîg Avatar asked Sep 07 '12 12:09

Orabîg


People also ask

Does Android have temporary files?

Temporary files are created after apps are installed and used, device updates are performed, and apps are removed. Before you know it, there are hundreds or even thousands of files that are no longer needed and that if left, can cause a host of problems.

Is it safe to delete temp files in Android?

Some apps store temporary files. You can free up space on your device by clearing these files. App settings are not affected. Important: If you clear junk files or delete files using Files by Google, the data will be permanently deleted.

Where are temp files stored in Android?

The temporary directory is /data/local/tmp .

Where do temporary files come from?

Temporary files, also called temp or tmp files, are created by Windows or programs on your computer to hold data while a permanent file is being written or updated. The data will be transferred to a permanent file when the task is complete, or when the program is closed.


2 Answers

try like this...

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

Edit :

for Question 2

It may be issue as below link...

Environment.getExternalStorageDirectory does not return the path to the removable storage

like image 102
Priyank Patel Avatar answered Oct 20 '22 17:10

Priyank Patel


you're getting this error because you declare on the manifest.xml permission to write to the external storage. you need to add this permission.

also, do never ever ever ever write files (specially temporary ones) directly on the getExternalStorage().

This will put directly on the SD-card and will create a mess. For temporary files that is only to be seen by your own application you should use:

getExternalStorage() + "/Android/data/<package_name>/cache/"

replacing by your app packaged, e.g. com. Orabig.myFirstApp that special folder is automatically deleted from the system if the user uninstall the application, keeping the system free from temporary files.

edit:

Please note that my manifest does not include the

you have to!

edit: also, if you creating temporary media files (PNG for example) is good practice to create an empty file named .nomedia on that folder. That way you avoid the Media Scanner scanning it and showing it on the gallery.

last edit:

and before creating files you must create the folder by calling mkdirs() on the File object.

like image 42
Budius Avatar answered Oct 20 '22 15:10

Budius