问题描述
好了,这是不完全的问题,因为我不是真的对我的code卡壳,但我发现有关访问外部存储设备和File.createTempFile一些奇怪的行为为Android API( )的方法,我想知道发生了什么......
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...
请注意,我明显的不提供<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE>
Please note that my manifest does not include the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
.
第1部分:
我有以下的code的确实工作一个魅力:
I have the following code which does work as a charm :
File tempFile = new File(Environment.getExternalStorageDirectory(),
"my_temp_file.png");
它为我创建一个临时文件,我可以写在其DATAS没有任何麻烦。
it creates a temporary file for me, and I can write datas in it without any trouble.
问题1 :为什么它的工作原理,因为我不应该在我的SD卡写权
Question 1 : Why does it work, as I'm not supposed to have writing rights on my SDCard ?
第2部分:
我试着改变我的code使用 createTempFile
这是创建临时文件的正式方法。所以,我已经试过:
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());
和添加的 WRITE_EXTERNAL_STORAGE 在我的manifest.xml。你猜怎么了 ?这不工作,我得到一个 java.io.IOException异常
:
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)
问2 :为什么这不工作,而恕我直言,它应该
Question 2 : Why this doesn't work, whereas imho it should ?
推荐答案
我终于想通了什么hapenning ...
I finally figured out what was hapenning...
问题1:事实上,将要使用的系统中的应用的创建是指该文件来检索和从该用户的库裁剪图像。所以,我的应用程序没有自己的WRITE_EXTERNAL_STORAGE权限,因为文件是由系统本身。
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.
问题2:我不知道。我想这是Android不允许创建一个临时文件,我试图(因为它是不正确的地方吧)
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)
这篇关于什么是Android上创建临时文件的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!