我发现了同样的问题。也许我们使用前置摄像头。我发现还有另一个可以使用相机并发现代码崩溃。
AndroidManifest.xml:
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/></manifest>
源代码:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private CameraView cv;
private Camera mCamera = null;
private Bitmap mBitmap = null;
public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.i("yao","onPictureTaken");
Toast.makeText(getApplicationContext(), "saving……", Toast.LENGTH_LONG).show();
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File file = new File("/sdcard/YY"+ new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg");
try {
file.createNewFile();
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
Toast.makeText(getApplicationContext(), "save completed", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
FrameLayout fl = new FrameLayout(this);
cv = new CameraView(this);
fl.addView(cv);
TextView tv = new TextView(this);
tv.setText("take a picture");
fl.addView(tv);
setContentView(fl);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("yao","MainActivity.onKeyDown");
if (keyCode == KeyEvent.KEYCODE_CAMERA) {
if (mCamera != null) {
Log.i("yao","mCamera.takePicture");
mCamera.takePicture(null, null, pictureCallback);
}
}
return cv.onKeyDown(keyCode, event);
}
class CameraView extends SurfaceView {
private SurfaceHolder holder = null;
public CameraView(Context context) {
super(context);
Log.i("yao","CameraView");
holder = this.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPreviewSize(854, 480);
parameters.setFocusMode("auto");
parameters.setPictureSize(2592, 1456);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
});
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}
}