Android Camera Preview Not Working (HTC MyTouch 3G Slide Android 2.2)
I can't see anything wrong with what I am doing, but I cannot get the camera on my phone to initialize and start drawing the preview. I have created a very simplified app that exhibits the same problem, shown below:
package com.ct;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class TestActivity extends Activity implements SurfaceHolder.Callback {
SurfaceHolder cameraPreviewHolder;
Camera camera;
SurfaceView cameraPreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cameraPreview = (SurfaceView) findViewById(R.id.SurfaceCamera);
cameraPreviewHolder = cameraPreview.getHolder();
cameraPreviewHolder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
camera = Camera.open();
camera.unlock();
Parameters p = camera.getParameters();
List<Size> sizes = p.getSupportedPreviewSizes();
int bestFit=0;
int difference = Integer.MAX_VALUE;
for(int i=0; i<sizes.size(); i++){
Size s = sizes.get(i);
int dif = (width-s.width)+(height-s.height);
if(s.width<=width && s.height<=height && dif>0 && dif < difference){
bestFit = i;
difference = dif;
}
}
Size s = sizes.get(bestFit);
p.setPreviewSize(s.开发者_如何学编程width, s.height);
p.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(p);
try {
camera.setPreviewDisplay(cameraPreviewHolder);
camera.startPreview();
} catch (IOException e) {
}
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
}
My logcat shows the following:
W/CameraSwitch( 2020): open main camera
W/CameraSwitch( 2020): no file - can't switch camera
E/QualcommCameraHardware( 68): native_access_parm: error (No such file or directory): fd 14, type 1, length 32, status 0
E/mm-camera 7x-vfe( 68): Received VFE start ACK!!! This is a user preview start.
E/CameraService( 68): registerBuffers failed with status -38
W/dalvikvm( 2020): threadid=1: thread exiting with uncaught exception (group=0x40028a00)
W/InputConnectionWrapper.ICC( 176): Timed out waiting on IInputContextCallback
Ideas? I just updated to Android 2.2 yesterday, and I hope this didn't break my camera API...
Have a look in the APIDemos/graphics/CameraPreview that comes with the SDK.
Alex
UPDATE: Try also changing from PixelFormat to ImageFormat. I noticed this was introduced in 2.2
精彩评论