1

I’m a beginner and I need help. How do I take photos with camera and save or send to next activity?

I've tried a couple of options, i.e. takepicture with picture callback and surfaceview/take with intent. However, neither works properly on Android 2.3.3. Could someone figure out the issues with my code below?

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sf_foto);
        mCamera = getCameraInstance();
        mCameraPreview = new SF_CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);

        ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCamera.takePicture(myShutterCallback, mPicture_RAW, mPicture);
            }
        });
    }

    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open(0);
            camera.setDisplayOrientation(90);
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    ShutterCallback myShutterCallback = new ShutterCallback(){
         @Override
         public void onShutter() {}
    };

    PictureCallback mPicture_RAW = new PictureCallback(){
         @Override
         public void onPictureTaken(byte[] arg0, Camera arg1) {}
    };

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } 
            Intent i = new Intent(StyloveFoto.this, Filter.class);
            startActivity(i);
        }
    };

    protected File getOutputMediaFile() {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"KWAlbum");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("KWAlbum", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "KW" + ".jpg");

        return mediaFile;
    }

my surface view:

public class SF_CameraPreview extends SurfaceView implements SurfaceHolder.Callback{

    private SurfaceHolder mSurfaceHolder;
    private Camera mCamera;

    public SF_CameraPreview(Context context, Camera camera) {
        super(context);
        this.mCamera = camera;
        this.mSurfaceHolder = this.getHolder();
        this.mSurfaceHolder.addCallback(this);
        this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        try {
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();
        } catch (IOException e) {
            // left blank for now
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        //mCamera.stopPreview();
        mCamera.release();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
        int width, int height) {
        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(surfaceHolder);
            Camera.Parameters parameters = mCamera.getParameters();
            parameters.set("orientation", "portrait");
            mCamera.setParameters(parameters);
            mCamera.startPreview();
        } catch (Exception e) {
            // intentionally left blank for a test
        }
    }
}
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Ares
  • 31
  • 1
  • 2
  • 8

3 Answers3

0

instead of this line : fos.write(data);

write : fos.write(data,0,data.length);

if you want to pass it to the next activity:

 Intent i = new Intent(StyloveFoto.this, Filter.class);
 i.putExtra("myImage",data);
 startActivity(i);

and then in class filter in the oncreate method

byte[] myImage = getIntent()getIntent().getByteArrayExtra("myImage");
Lena Bru
  • 13,521
  • 11
  • 61
  • 126
  • There is no difference between fos.write(data) and fos.write(data,0,data.length). FileOutputStream has two methods to write byte array. – user1991679 Apr 25 '14 at 13:59
0

I got the same problem and i just solve it before. The problem is that mPicture is an object of PictureCallback. You can't set intent directing to Filter.class from StyloveFoto.this, because it is in PictureCallback interface. Try this one:

Intent i = new Intent(getBaseContext() , Filter.class);
startActivity(i);

Such a big trap in java....hope it helps :)

0

Either you save the picture like this and pass the path to another activity

final File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + "_pic.jpg");
        OutputStream output = null;
        try {
            output = new FileOutputStream(file);
            output.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != output) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

OR Pass data from camera activity :

Intent intent = new Intent(getApplicationContext(), your_class.class);
    intent.putExtra("path", path);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

Receiving Activity

byte[] data = getIntent().getByteArrayExtra("path");
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
image.setImageBitmap(scaleDownBitmapImage(bitmap, 300, 200));
Jins Lukose
  • 699
  • 6
  • 19