简体   繁体   中英

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 ?

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

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, eg 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM