Cannot connect to camera service while using ACTION_IMAGE_CAPTURE

I use the following code to tell the system that I want to take a snapshot:

Intent intent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE, null); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri .fromFile(new File(filePath))); startActivityForResult(intent, TAKE_PHOTO_ACTIVITY); 

He works as a champion for the first time. Subsequent attempts raise the following exception:

E / CameraHolder (8300): java.lang.RuntimeException: cannot connect to the camera service E / CameraHolder (8300): with android.hardware.Camera.native_setup (Native Method) E / CameraHolder (8300): with android.hardware. Camera (Camera.java:110) E / CameraHolder (8300): with android.hardware.Camera.open (Camera.java:90) E / CameraHolder (8300): with com.android.camera.CameraHolder.open (CameraHolder.java : 100) E / CameraHolder (8300): when com.android.camera.Camera.ensureCameraDevice (Camera.java:1626) E / CameraHolder (8300): when com.android.camera.Camera.startPreview (Camera.java:1686 ) E / CameraHolder (8300): with com.android.camera.Camera.access $ 5800 (Camera.java:94) E / CameraHolder (8300): with com.android.camera.Camera $ 5.run (Camera.java:949 ) E / CameraHolder (8300): with java.lang.Thread.run (Thread.java:1096)

I suppose I need to somehow let go of the camera object, but since I don't get it directly, I have no idea how to do this. Can someone help me?

+6
android
source share
3 answers

You do not need to let go of the camera object - this is even impossible, because you do not have a handle. This object is freed inside the capture operation that you are invoking.

Do you always use the same file path? If so, try generating a unique one each time. If this does not help, it looks like a device error.

+1
source share
 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

to invoke the default camera activity.

and use the following code to retrieve the snapshot just taken:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_PIC_REQUEST) { Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); ImageView image = (ImageView) findViewById(R.id.photoResultView); image.setImageBitmap(thumbnail); } } 

and add permission to the manifest file

 <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.CAMERA"></uses-permission> 

Hope this work is for you.

+1
source share

I also had the same problem. Then I came up with this solution and it works great for me.

When you invoke camera activity, the temporary file on the memory card is transferred to EXTRA_OUTPUT. When the camera function returns, in the onActivityResult () callback method, get this temporary file created earlier and add it to MediaStore. At the end, delete the temporary file.

+1
source share

All Articles