我在尝试使用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;
}
}
2条答案
按热度按时间mrphzbgm1#
在下面的代码部分中。需要使用try catch来处理错误。
这是一个简化的示例:
作为参考,这里有一个指向CameraAccessException类的链接。
xwmevbvl2#