zba
zba

Reputation: 113

android.hardware.camera2.CameraAccessException

I'm receiving this error message while trying get the camera ID list from a CameraManager instance using the camera2 api:

UnhandledException: android.hardware.camera2.CameraAccessException

Here is my code

    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    String cameraId = manager.getCameraIdList()[0];

I'm receiving the error message on the second line. I've set my camera permissions in the manifest and even made sure my physical phone was on, connected and set up for developer mode (even though I don't think that should have any impact), but can't think of anything else that is different from all the simple examples I've seen online.

Below is the full MainActivity code:

Any advice is appreciated...

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraAccessException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.internal.app.ToolbarActionBar;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toolbar;

import com.google.zxing.integration.android.IntentIntegrator;

import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.List;

import Model.Consumer;
import Model.Plan;
import Model.Product;
import Model.ProductInfo;
import Model.Retailer;
import Model.Transaction;


public class MainActivity extends AppCompatActivity {

private android.support.v7.widget.Toolbar toolbar;

private RecyclerView recList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    recList = (RecyclerView) findViewById(R.id.cardList);
    //recList.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    recList.setHasFixedSize(true);

    // use a linear layout manager
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    ProductAdapter pa = new ProductAdapter(createDummyProduct());
    recList.setAdapter(pa);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean 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) {
        return true;
    }
    else if (id == R.id.action_add) {

    }

    return super.onOptionsItemSelected(item);
}

public void scanBarcodeFrontCamera(View view) {
    if (hasCamera(this)) {
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        String cameraId = manager.getCameraIdList()[0];

    }
    //IntentIntegrator integrator = new IntentIntegrator(this);
    //integrator.setCameraId(CameraDevice);
    //integrator.initiateScan();
}

private boolean hasCamera(Context context) {
//        if     (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
//            // this device has a camera
//            return true;
//        } else {
//            // no camera on this device
//            return false;
//        }
        return true;
    }
}

Upvotes: 6

Views: 13090

Answers (2)

Solanki Kamlesh
Solanki Kamlesh

Reputation: 114

    try {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)    
    {   
        mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mCameraId = mCameraManager.getCameraIdList()[0];
        }
    }

    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 0

user1861582
user1861582

Reputation: 119

In the section of your code below. A try catch is required to handle errors.

CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String cameraId = manager.getCameraIdList()[0];

This is a simplified example:

CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

try {
   cameraIdList = manager.getCameraIdList();
} catch (CameraAccessException e){
   e.printStackTrace();
}

For reference, here is a link to the CameraAccessException class.

Upvotes: 0

Related Questions