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 ?
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.
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.
The temporary directory is /data/local/tmp .
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.
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, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With