i followed the tutorial in this forum and wrote some lines two capture an image with the camera:
Using java Syntax Highlighting
- package prototype.camera;
- import java.io.IOException;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Intent;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore.Images.Media;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.MenuItem;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- public class ImageCapture extends Activity implements SurfaceHolder.Callback {
- private Camera camera;
- private boolean isPreviewRunning = false;
- private SurfaceView surfaceView;
- private SurfaceHolder surfaceHolder;
- private Uri target = Media.EXTERNAL_CONTENT_URI;
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- Log.e(getClass().getSimpleName(), "onCreate");
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- setContentView(R.layout.main);
- surfaceView = (SurfaceView)findViewById(R.id.surface);
- surfaceHolder = surfaceView.getHolder();
- surfaceHolder.addCallback(this);
- surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
- public boolean onCreateOptionsMenu(android.view.Menu menu) {
- MenuItem item = menu.add(0, 0, 0, "goto gallery");
- item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
- public boolean onMenuItemClick(MenuItem item) {
- Intent intent = new Intent(Intent.ACTION_VIEW, target);
- startActivity(intent);
- return true;
- }
- });
- return true;
- }
- @Override
- protected void onRestoreInstanceState(Bundle savedInstanceState) {
- super.onRestoreInstanceState(savedInstanceState);
- }
- Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
- public void onPictureTaken(byte[] data, Camera c) {
- Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
- camera.startPreview();
- }
- };
- Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
- public void onPictureTaken(byte[] data, Camera c) {
- Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
- }
- };
- Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
- public void onShutter() {
- Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
- }
- };
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- ImageCaptureCallback iccb = null;
- if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
- try {
- String filename = "testName";
- ContentValues values = new ContentValues();
- values.put(Media.TITLE, filename);
- values.put(Media.DISPLAY_NAME, filename);
- values.put(Media.DESCRIPTION, "Image capture by camera");
- Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
- //String filename = timeStampFormat.format(new Date());
- iccb = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));
- } catch(Exception ex ){
- ex.printStackTrace();
- Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
- }
- }
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- return super.onKeyDown(keyCode, event);
- }
- if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
- camera.takePicture(mShutterCallback, mPictureCallbackRaw, iccb);
- return true;
- }
- return false;
- }
- protected void onResume() {
- Log.e(getClass().getSimpleName(), "onResume");
- super.onResume();
- }
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- }
- protected void onStop() {
- Log.e(getClass().getSimpleName(), "onStop");
- super.onStop();
- }
- public void surfaceCreated(SurfaceHolder holder) {
- Log.e(getClass().getSimpleName(), "surfaceCreated");
- camera = Camera.open();
- }
- public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
- Log.e(getClass().getSimpleName(), "surfaceChanged");
- if (isPreviewRunning) {
- camera.stopPreview();
- }
- Camera.Parameters p = camera.getParameters();
- p.setPreviewSize(w, h);
- camera.setParameters(p);
- try {
- camera.setPreviewDisplay(holder);
- } catch (IOException e) {
- e.printStackTrace();
- }
- camera.startPreview();
- isPreviewRunning = true;
- }
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.e(getClass().getSimpleName(), "surfaceDestroyed");
- camera.stopPreview();
- isPreviewRunning = false;
- camera.release();
- }
- }
Parsed in 0.049 seconds, using GeSHi 1.0.8.4
and the second file
Using java Syntax Highlighting
- package prototype.camera;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
- import android.hardware.Camera;
- import android.hardware.Camera.PictureCallback;
- public class ImageCaptureCallback implements PictureCallback {
- private OutputStream filoutputStream;
- private String fileName = "test";
- public ImageCaptureCallback(OutputStream filoutputStream) {
- this.filoutputStream = filoutputStream;
- }
- @Override
- public void onPictureTaken(byte[] data, Camera camera) {
- try {
- FileOutputStream buf = new FileOutputStream("/sdcard/dcim/Camera/" + fileName + ".jpg");
- buf.write(data);
- buf.flush();
- buf.close();
- filoutputStream.flush();
- filoutputStream.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
My problem is twofold. First the filename is always a long number, and not test as i expected. then i can't really create a file, because there is always an illegalstateexpcetion....
any hints??
thanks a lpt!

