user1634451 user1634451 - 2 years ago 139
Android Question

Api 23 take picture without storage permissions

I need to take pictures using the camera intent in api 23. I am requesting permission to use the camera intent but in an effort to limit the number of permission I need to check or ask for I don't want to save the picture in an app internal storage. But passing file like this to the camera intent doesn't work. I am guessing cause the camera can't write to my apps storage.

try {
mTempImage = File.createTempFile("psn_temp", ".jpg", Controller.getContext().getCacheDir());
} catch (IOException e) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempImage));

if i do

File storageDir = Environment.getExternalStorageDirectory();
try {
mTempImage = File.createTempFile(
"psn_temp", /* prefix */
".jpg", /* suffix */
storageDir /* directory */

this works but i have to ask for storage permission. Is there a way around this?

Answer Source

Use Context.getExternalFilesDir(String) for saving picture to your local external folder. Since KitKat (api 19) you dont need permissions to write to this external storage folder. Before api19 you had to require ​R​E​A​D​_​E​X​T​E​R​N​A​L​_​S​T​O​R​A​G​E​, so if you want to support earlier apis add to your manifest:

​ ​ ​ ​<​u​s​e​s​-​p​e​r​m​i​s​s​i​o​n​ ​a​n​d​r​o​i​d​:​n​a​m​e​=​"​a​n​d​r​o​i​d​.​p​e​r​m​i​s​s​i​o​n​.​R​E​A​D​_​E​X​T​E​R​N​A​L​_​S​T​O​R​A​G​E​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​m​a​x​S​d​k​V​e​r​s​i​o​n​=​"​1​8​"​
 ​ ​ ​ ​ ​ ​ ​ ​/​>​

you also dont need WRITE_* permission, since you are not writing - camera app is doing this.

For explanation why getCacheDir does not work look here:

Camera Intent not saving photo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download