android.hardware.camera2.CameraAccessException

时间:2015-08-03 16:49:48

标签: android android-camera-intent

我在尝试使用camera2 api从CameraManager实例获取摄像机ID列表时收到此错误消息:

UnhandledException:android.hardware.camera2.CameraAccessException

这是我的代码

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

我在第二行收到错误消息。我已经在清单中设置了我的相机权限,甚至确保我的物理手机已开启,连接并设置为开发者模式(即使我认为这不会产生任何影响),但可以&#39我想到的任何其他与我在网上看到的所有简单例子都不同。

以下是完整的MainActivity代码:

感谢任何建议......

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;
    }
}

2 个答案:

答案 0 :(得分:1)

在下面的代码部分。需要try catch来处理错误。

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

这是一个简化的例子:

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

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

供参考,这是指向CameraAccessException 类的链接。

答案 1 :(得分:0)

    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();
    }