有沒有辦法給我的應用添加永久性的內置攝像頭權限?我的應用程序需要立即工作,因爲我們不希望用戶記住每一個煩人的應該初始化應用程序(應該是自動的)。我增加了以下我AndroidManifest,但似乎並沒有工作:
<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>
這裏是我的代碼,我嘗試打開攝像機,但程序抱怨說,我需要的權限代碼(我註釋掉因爲我不希望它):
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);
}
}
「我們不希望用戶記得每次都急於初始化應用程序」 - 他們只需在第一次運行應用程序時授予運行時權限一次。 – CommonsWare
我明白了。也許我總是看到權限,因爲我每次更改代碼中的某些內容時都必須重新安裝應用程序。好點子。 – Pototo