I have code that I inherited that worked in < api 23. I had to upgrade it to work with the latest apis, and it necessitated me to use a FileProvider now, due to a exposed fil
Just an fyi, the previous call before the upgrade looked like
That code uses EXTRA_OUTPUT
, which is the correct way to pass the Uri
with ACTION_IMAGE_CAPTURE
. Its problem is the Uri.fromFile()
part, which will not work on Android 7.0+, once your targetSdkVersion
rises to 24 or higher, as you noted.
The correct code is the combination of the two:
File f = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(MainActivity.this, AUTHORITY, f));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CAMERA);
See this sample app for a complete example of using ACTION_IMAGE_CAPTURE
with FileProvider
.