Camera Intent在活动中启动

时间:2011-09-08 12:31:47

标签: android android-camera android-service

我有一个名为MainActivity的活动和一个名为CameraService的服务,它绑定到套接字端口。每当套接字收到字符串“camera”时,服务就会广播到MainActivity以启动摄像头。

Intent launchIntent = new Intent(MainActivity._LAUNCH_CAMERA_ACTION);
sendBroadcast(launchIntent);

MainActivity注册_LAUNCH_CAMERA_ACTION和BroadcastReceiver。

IntentFilter filter = new IntentFilter();
filter.addAction(_LAUNCH_CAMERA_ACTION);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
       if (_LAUNCH_CAMERA_ACTION.equalsIgnoreCase(intent.getAction())) {
         Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     MainActivity.this.startActivityForResult(cameraIntent, REQUEST_CAMERA);
       }
}
}, filter);

每当调用相机意图时,程序都会出现未处理的异常,并且由于异常而终止。

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

引用this帖子即可:

     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");  
     startActivityForResult(intent, 0);

在清单文件中添加Camers Uses Permission。

 <uses-permission android:name="android.permission.CAMERA"></uses-permission>