开发者

Cannot connect to camera service when using ACTION_IMAGE_CAPTURE

I'm using the following code to tell the system I want to take a picture:

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

It works like a champ, the first time. Subsequent tries yield the following exception:

E/CameraHolder( 8300): java.lang.RuntimeException: Fail to connect to camera service E/CameraHolder( 8300): at android.hardware.Camera.native_setup(Native Method)开发者_如何转开发 E/CameraHolder( 8300): at android.hardware.Camera.(Camera.java:110) E/CameraHolder( 8300): at android.hardware.Camera.open(Camera.java:90) E/CameraHolder( 8300): at com.android.camera.CameraHolder.open(CameraHolder.java:100) E/CameraHolder( 8300): at com.android.camera.Camera.ensureCameraDevice(Camera.java:1626) E/CameraHolder( 8300): at com.android.camera.Camera.startPreview(Camera.java:1686) E/CameraHolder( 8300): at com.android.camera.Camera.access$5800(Camera.java:94) E/CameraHolder( 8300): at com.android.camera.Camera$5.run(Camera.java:949) E/CameraHolder( 8300): at java.lang.Thread.run(Thread.java:1096)

I imagine I have to somehow release the camera object, but since I'm not directly acquiring it, I have no idea how to do this. Can someone help me out?


You do not need to release the camera object - it is even impossible because you do not have a handle to it. This object is released inside the capture activity you are calling.

Are you always using the same file path? If yes try to generate a unique one each time. If this do not help, it looks evidently like a device specific bug.


Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  

to call default camera activity.

and use next code to retrieve the picture 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 in your 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 for you.


I also had the same problem. Then I came up with this solution, and it is working fine for me.

When you call the camera activity, in EXTRA_OUTPUT pass an Uri of a temp file on the storage card. When the camera activity come back, in the onActivityResult() callback method, get that temp file created previously, and add it to the MediaStore. At the end delete the temp file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜