I am trying to take photo from android device's native Camera App. For that i create a file first and then attach its URI with the intent to capture image and write output in that file. For file provider i have added the following in the AndroidManifest file
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
And the java code for capturing image is as follows
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(mActivity.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
String imageName = "image_" + Preferences.getStringForUserId(mActivity);
photoFile = createImageFile(imageName);
mCurrentPhotoPath = photoFile.getAbsolutePath();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
try {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String authorities = "com.example.myapp.fileprovider";
Uri photoURI = FileProvider.getUriForFile(mActivity, authorities, photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
} else {
Uri photoURI = Uri.fromFile(photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
}
mActivity.startActivityForResult(takePictureIntent, CAMERA_CODE);
} catch (Exception ex) {
// Error occurred while creating the File
ex.printStackTrace();
Toast.makeText(mActivity, ex.toString(), Toast.LENGTH_LONG).show();
}
}
} else {
Toast.makeText(mActivity, "No Camera App found in device", Toast.LENGTH_LONG).show();
}
As documented in Android own documentation by Google in: https://developer.android.com/training/camera/photobasics.html I have added necessary permissions
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
But did not add permission for Camera and i am also asking for runtime permissions for Android M or higher. Getting from the Google docs it is clear that we do not need Camera Permission if we want to use native Camera App to capture photos for our App, then why does it give me this exception stated below
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/Android/data/com.example.myapp/files/Pictures/myapp/image_20130-1383873550.jpg} (has extras) } from ProcessRecord{d82d864 6641:com.pencilinapp.pencilin/u0a63} (pid=6641, uid=10063) with revoked permission android.permission.CAMERA
So my question is that do i have to ask for both Camera and Storage permissions from now on? And why is it not stated in Google Documentation?
I have researched and checked many stackoverflow threads but none gave answer related to this issue so far.