Reputation: 701
Folks,
Is there a way to add permanent built in camera permission to my app? My app needs to work right away cause we don't want the user remember to initialize the app every morming (should be automatic). I added the following to my AndroidManifest, but does not seem to work:
<uses-permission
android:required="true"
android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera2.full" />
<uses-feature android:name="android.hardware.camera2.autofocus" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application
<activity
android:name="com.serenegiant.usbcameratest5.MainActivity"
android:label="@string/app_name"
android:screenOrientation="sensorPortrait"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.camera2.CameraDevice" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.camera2.CameraManager" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.camera2.CameraCharacteristics" />
</intent-filter>
<meta-data
android:name="android.hardware.camera2.CameraManager"
android:resource="@xml/device_filter" />
<meta-data
android:name="android.hardware.camera2.CameraDevice"
android:resource="@xml/device_filter" />
<meta-data
android:name="android.hardware.camera2.CameraMetadata"
android:resource="@xml/device_filter" />
</activity>
</application>
And here is my code where I try to open the camera, but the program complains that I need the permission code (which I commented out cause I don't want it):
private void openCamera(int width, int height) {
// if (ContextCompat.checkSelfPermission(mWeakParent.clear(), Manifest.permission.CAMERA)
// != PackageManager.PERMISSION_GRANTED) {
// requestCameraPermission();
// return;
// }
setUpCameraOutputs(width, height);
configureTransform(width, height);
Activity activity = mWeakParent.get();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}
}
Upvotes: 0
Views: 6959
Reputation: 6114
If you do not want to use the run-time permission( though its a bad practice), then you can lower your target API in build.gradle
file:
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.xaugmentedreality.arproject"
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
}
This keeps the build tools to the latest version, but the app would be targeted for API version 22 and below. Thus you wouldn't need to use run time permission. Though you'd still need the permission explicitly declared in manifest file:
<uses-permission android:name="android.permission.CAMERA" />
PS:
I do use this way for one of my app, as it does image processing and targeting for Version > 23 with run time permission causes crashes in Mi and Asus devices, and the hardest part is the app crashes before even an error reporting tool like Acra or Crashalytics would run. So this should work, but not a recommended option, only if your requirement suffice it.
Upvotes: 1