Reputation: 149
My fragment is trying to start Camera Activity, but it seems like there is some camera permission problem,
while debugging it goes till below line of code only
startActivityForResult(callCameraApplicationIntent, CAMERA_PIC_REQUEST);
Below is the logcat result
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=android/com.android.internal.app.ResolverActivity clip={text/uri-list U:content://com.example.tc.provider/external_files/Pictures/photo_saving_app/IMAGE_20191205_070208.jpg} (has extras) } from ProcessRecord{8920903 18662:com.example.tc/u0a343} (pid=18662, uid=10343) with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:2016)
at android.os.Parcel.readException(Parcel.java:1962)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4452)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1617)
at android.app.Activity.startActivityForResult(Activity.java:4551)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
Following is the part of code That seems required.
if (Build.VERSION.SDK_INT > 21) { //use this if Lollipop_Mr1 (API 22) or above
Intent callCameraApplicationIntent = new Intent();
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
// We give some instruction to the intent to save the image
File photoFile = null;
try {
// If the createImageFile will be successful, the photo file will have the address of the file
photoFile = createImageFile();
// Here we call the function that will try to catch the exception made by the throw function
} catch (IOException e) {
Logger.getAnonymousLogger().info("Exception error in generating the file");
e.printStackTrace();
}
// Here we add an extra file to the intent to put the address on to. For this purpose we use the FileProvider, declared in the AndroidManifest.
Uri outputUri = FileProvider.getUriForFile(
getActivity(),
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
// The following is a new line with a trying attempt
callCameraApplicationIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Logger.getAnonymousLogger().info("Calling the camera App by intent");
// The following strings calls the camera app and wait for his file in return.
startActivityForResult(callCameraApplicationIntent, CAMERA_PIC_REQUEST);
Manifest file is below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tc">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<!--
Allows Glide to monitor connectivity status and restart failed requests if users go from a
a disconnected to a connected network state.
-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="Login"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>
It was working fine previously. I might have done some silly mistake as I am just a learner with help of internet only.
Upvotes: 4
Views: 5382
Reputation: 199805
As per the ACTION_IMAGE_CAPTURE
documentation:
Note: if you app targets M and above and declares as using the
Manifest.permission.CAMERA
permission which is not granted, then attempting to use this action will result in aSecurityException
.
You do not need the camera permission to use ACTION_IMAGE_CAPTURE
on any API level. Therefore, if you are only using ACTION_IMAGE_CAPTURE
, you can remove the line <uses-permission android:name="android.permission.CAMERA" />
.
If you are using the Camera APIs in addition to using ACTION_IMAGE_CAPTURE
, you must request the permission at runtime.
Upvotes: 11