android createTempFile throws permission denied?

You need to create the temp files in a directory that your application owns. You should use createTempFile(String prefix, String suffix, File directory), where directory is the location to which the temp file is to be written. You can get a valid location for directory with the result from Context.getFilesDir() or Context.getDir(String name, int mode).


Even after adding <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" this to manifest I was getting error. But after adding this android:requestLegacyExternalStorage="true" in application tag of manifest file I am able to createTempFile and able capture pic from phone.

<application android:requestLegacyExternalStorage="true"


I think that you just missed the permission to write at the external storage, since temp files are created there by default. Add

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

to your manifest and it should work.