我在尝试使用Camera2 API从CameraManager实例获取相机ID列表时收到此错误消息:
未处理的异常:android.hardware.camera2.cameraAccessException
这是我的密码

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

我在第二行收到错误信息。我已经在清单中设置了我的相机权限,甚至确保我的物理电话已打开、连接并设置为开发模式(尽管我认为这不会有任何影响),但我想不出还有什么不同于我在网上看到的所有简单示例的。
以下是完整的主活动代码:
任何建议都是值得赞赏的…
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;
    }
}

最佳答案

在下面的代码部分。需要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 类的链接。

08-18 07:10