[Xperia Z1 Original Android Development] drklaw:
Did I Find A Potential Camera Issue Workaround?

Hello there,

This is basically a call for a bit of dev help for a potential workaround I think I may have stumbled upon for fixing the annoying hanging camera issue we seem to have on a bunch of the CM/AOSP based roms. The symptoms of the issue are:

1. Reboot phone

2. Open any camera app - there's a frozen frame for a moment then it either

a. works normally

b. freezes and camera apps then "cannot connect to camera" until you reboot and try again.

The issue I was seeing on my phone (running the awesome SlimKat ROM) was that sometimes it would end up at 2a, and sometimes at 2b. I could not see any pattern, rhyme, or reason for it. Until...

I opened the ROM's camera app (set to store on my SD card) with my SD card removed. This caused the camera app to crash before showing any camera preview. I then noticed that once I put the SD card back in the camera app would open and work perfectly and immediately. No freezing at all. I have replicated about 20 times successfully, with no instances of 2b.

That made me think. I've done some Android head tracking coding in the past and worked with camera previews and I know that to get a camera preview running, you first open the camera object then start the preview. Since normally, sometimes the situation would be 2a and sometimes 2b, that made me think that there might be a race condition happening between the "camera open" step and the "show preview" step.

So I made a simple app to open the camera, wait 5 seconds, and close, without showing any preview. I set this app to run on boot-up using Tasker. (This is meant to replace the manual SD card trick I was using, outlined above.) So far, I've run it about 5 times and the camera has been starting right up for me.

I'm sharing this with the hope that the ROM developers, who are far more skilled than I, might use this information. I attached my apk, and I'll give you the code. I hope you can use it to confirm or reject my findings or even do a better job integrating it into your ROM then I did with my boot-up hack.

Thanks!

(I really love this ROM, by the way.)

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;

public class MainActivity extends Activity implements SurfaceTextureListener{

private Camera mCamera;
private TextureView mTextureView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);

setContentView(mTextureView);
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCamera = Camera.open();

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
mCamera.release();

android.os.Process.killProcess(android.os.Process.myPid());
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, the Camera does all the work for us
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
return true;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Update your view here!
}
}