createTempFile throws "Permission denied" exception
My app. fails on the line:
File temp = File.createTempFile(“mediaplayertmp”, “dat”);
throws “Perm开发者_如何转开发ission denied”
exception.
Why could this occur?
It sounds like I need to change some kind of Java setting to designate a “remp
” folder.
Thanks!
It's probably that you don't have sufficient permissions to write to an external storage directory. I don't know that createTempFile
requires this (I would think it would be on internal storage, but don't know that), but I would suggest adding the WRITE_EXTERNAL_STORAGE permission to your manifest, like so:
<manifest...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
I had this same issue. For me it turned out the problem was that I had my Android device plugged into my PC via USB while I was developing, and the SD card is locked when it is mounted by another device. Once I unplugged the device, it worked fine. You can also tell the device not to mount the SD card when plugged in.
Since AFAIK the default temp file location as configured by java.io.tmpdir
is /sdcard
, you'll need to add the WRITE_EXTERNAL_STORAGE permission to your manifest.
精彩评论