• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android “Permission Denial: can't use the camera”

I'm following a tutorial on utilizing the camera in an Android app. I am getting the error "Permission Denial: can't use the camera" when running debug, both on emulator and physical device. I have tried a variety of permissions in my manifest file. It seems most people who have had this error have had a typo, a missing permission, or the permissions not in the right place in their manifest.

Here's my manifest file:

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.karudo.dbzrealpowerup">

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permissionandroid:name="android.permission.FLASHLIGHT"/>
<uses-permissionandroid:name="android.permission.CAMERA"/>
<uses-featureandroid:name="android.hardware.camera2"/>

<application
android:allowBackup="true"
android:icon="@Mipmap/ic_launcher"
android:label="@String/app_name"
android:theme="@Style/AppTheme">
<activity
android:name=".DBZHome"
android:label="@String/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".DBZStartPowerUp"
android:label="@String/title_activity_dbzstart_power_up">
</activity>
</application>

</manifest>
Here's my activity:

package com.example.karudo.dbzrealpowerup;

import android.app.Activity;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Bundle;
import android.util.Size;
import android.view.Menu;
import android.view.MenuItem;
import android.view.TextureView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

publicclassDBZStartPowerUpextendsActivity{

privateSize mPreviewSize;
privateString mCameraId;
privateTextureView mTextureView;
privateTextureView.SurfaceTextureListener mSurfaceTextureListener =
newTextureView.SurfaceTextureListener(){

@override
publicvoid onSurfaceTextureAvailable(SurfaceTexture surface,int width,int height){
setupCamera(width, height);
openCamera();
}

@override
publicvoid onSurfaceTextureSizeChanged(SurfaceTexture surface,int width,int height){

}

@override
publicboolean onSurfaceTextureDestroyed(SurfaceTexture surface){
returnfalse;
}

@override
publicvoid onSurfaceTextureUpdated(SurfaceTexture surface){

}
};

privateCameraDevice mCameraDevice;
privateCameraDevice.StateCallback mCameraDeviceStateCallback
=newCameraDevice.StateCallback(){

@override
publicvoid onOpened(CameraDevice camera){
mCameraDevice = camera;
Toast.makeText(getApplicationContext(),"Camera Opened!",Toast.LENGTH_SHORT).show();
}

@override
publicvoid onDisconnected(CameraDevice camera){
camera.close();
mCameraDevice =null;
}

@override
publicvoid onError(CameraDevice camera,int error){
camera.close();
mCameraDevice =null;
}
};

@override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbzstartpowerup);
mTextureView =(TextureView) findViewById(R.id.dbzCameraPreview);
}

@override
publicvoid onResume(){
super.onResume();
if(mTextureView.isAvailable()){

}else{
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}

@override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dbzstartpowerup, menu);
returntrue;
}

@override
publicboolean onOptionsItemSelected(MenuItem item){
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if(id == R.id.action_settings){
returntrue;
}

returnsuper.onOptionsItemSelected(item);
}

privatevoid setupCamera(int width,int height){
CameraManager cameraManager =(CameraManager) getSystemService(Context.CAMERA_SERVICE);
try{
for(String cameraId : cameraManager.getCameraIdList()){
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING)==
CameraCharacteristics.LENS_FACING_FRONT){
continue;
}
StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
mPreviewSize = getPreferredPreviewSize(map.getOutputSizes(SurfaceTexture.class), width, height);
mCameraId = cameraId;
return;
}
}catch(CameraAccessException e){
e.printStackTrace();
}
}

privateSize getPreferredPreviewSize(Size[] mapSizes,int width,int height){
List<Size> collectorSizes =newArrayList<>();
for(Size option : mapSizes){
if(width > height){
if(option.getWidth()> width &&
option.getHeight()> height){
collectorSizes.add(option);
}
}else{
if(option.getWidth()> height &&
option.getHeight()> width){
collectorSizes.add(option);
}
}
}
if(collectorSizes.size()>0){
returnCollections.min(collectorSizes,newComparator<Size>(){
@override
publicint compare(Size lhs,Size rhs){
returnLong.signum(lhs.getWidth()* lhs.getHeight()- rhs.getWidth()* rhs.getHeight());
}
});
}
return mapSizes[0];
}

privatevoid openCamera(){
CameraManager cameraManager =(CameraManager) getSystemService(Context.CAMERA_SERVICE);
try{
cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback,null);
}catch(CameraAccessException e){
e.printStackTrace();
}
}
}
And the error in my logcat:

10-0403:15:02.740961-8780/? E/CameraService﹕PermissionDenial: can't use the camera pid=20601, uid=10059
10-04 03:15:02.741 20601-20601/com.example.karudo.dbzrealpowerup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.karudo.dbzrealpowerup, PID: 20601
java.lang.SecurityException: Lacking privileges to access camera service
at android.hardware.camera2.utils.CameraBinderDecorator.throwOnError(CameraBinderDecorator.java:108)
at android.hardware.camera2.legacy.CameraDeviceUserShim.connectBinderShim(CameraDeviceUserShim.java:336)
at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:324)
at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:454)
at com.example.karudo.dbzrealpowerup.DBZStartPowerUp.openCamera(DBZStartPowerUp.java:163)
at com.example.karudo.dbzrealpowerup.DBZStartPowerUp.access$100(DBZStartPowerUp.java:23)
at com.example.karudo.dbzrealpowerup.DBZStartPowerUp$1.onSurfaceTextureAvailable(DBZStartPowerUp.java:34)
at android.view.TextureView.getHardwareLayer(TextureView.java:368)
at android.view.View.updateDisplayListIfDirty(View.java:15151)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I'm new to app development and admittedly I'm no good at debugging, but my manifest permissions appear to be correct from what I've seen of other people's files (and the tutorial which is only 4 months old).

Can anyone please tell me what I've done wrong?

Cheers, Lee.
 
Application Development thread moved to the Development forum for better exposure.:)
 
Back
Top Bottom