4

I am facing issue while runing camera on Moto E. On other devices my code works well but not on Moto E. It might because of the reason that Moto E doesn't have flash or secondary camera.

private synchronized void startCameraPreview() {


        try {
            previewView.getHolder().addCallback(this);
            previewView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            camera.setPreviewCallback(null);

            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();

        }
        catch (Exception exception) {
            Log.e(TAG, "Can't start camera preview due to Exception", exception);

            listener.onCameraError();
        }
    }

This gives error which is pasted below:

Can't start camera preview due to Exception
 java.io.IOException: setPreviewTexture failed
at android.hardware.Camera.setPreviewDisplay(Native Method)
    at android.hardware.Camera.setPreviewDisplay(Camera.java:474)
    at CameraFragment.startCameraPreview(CameraFragment.java:149)
    at CameraFragment.surfaceCreated(CameraFragment.java:472)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:572)
    at android.view.SurfaceView.access$000(SurfaceView.java:86)

If anybody knows how to deal with this issue?

Nidhi
  • 699
  • 13
  • 26
  • http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails check this, may be this would help you – Pratik Dasa Jun 11 '14 at 09:55
  • Thanks for reply. I already had tried dis. but found no luck. – Nidhi Jun 11 '14 at 10:11
  • is it found useful or not? That thing you forgot to tell me I think – Pratik Dasa Jun 11 '14 at 10:11
  • no. I have used (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS),but couldn't find any workaround. – Nidhi Jun 11 '14 at 10:15
  • on given url above, have you tried that code? I mean have you replace that code with yours? – Pratik Dasa Jun 11 '14 at 10:16
  • I don't think it is good idea to replace all my code . just want to find out the error in my existing code. – Nidhi Jun 11 '14 at 10:18
  • But I think its not generalize error, its just coming it in one device only, so may be there is some issue with the device's callback. So as per my thinking you have to try with some other code. – Pratik Dasa Jun 11 '14 at 10:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55421/discussion-between-pratik-and-nidhi). – Pratik Dasa Jun 11 '14 at 10:24
  • paste sample code in discussion chat – Pratik Dasa Jun 11 '14 at 10:41
  • 9
    I resolved this error by using camera.stopPreview(); just before camera.setPreviewDisplay() . Hope this will be useful to someone else. :) – Nidhi Jun 21 '14 at 07:25
  • Batter you answer your own question, it will help others because not everyone read the comments – Rahul Sonone Jul 18 '16 at 08:59
  • 1
    camera.stopPreview(); yes @Nidhi it is usefull it solved 2 issues : 1. Camera preview was stop, now its working fine. 2. setPreviewTexture failed Exception. – Rahul Sonone Jul 18 '16 at 09:03

1 Answers1

6

I resolved this error by using camera.stopPreview(); just before camera.setPreviewDisplay() . Hope this will be useful to someone else.

Just to document Nidhi's solution as an answer for those who didn't read the comments.

The root of the problem, though, still remains inexplicable to me. I tried camera.stopPreview as well as camera.release in the onPause() (as I was getting the crash after the camera was resumed), but apparently it didn't work until I tried the aforementioned solution.

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
  • 1
    Hi i was facing the same problem. By implementing the aforementioned solution, i was still getting setPreviewTexture failed Exception. I fixed the issue by using camera.stopPreview(); in onStop method of my activity, as i was getting the error when the app goes in background and is returned back. – Uday Khatry Jan 03 '18 at 07:02