Question

I am making a camera app that takes an image when the camera is first initiated, I have got it working, however the only problem I am getting is that when a picture is taken, it stores the first image in the folder. EG. I install the app, the camera takes a photo, then the next time the camera takes another photo, it is the same as the first one. I have tried to make the data = null after, however this is still not working.

Any suggestions?

package com.example.charlie;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;



public class PService2 extends Service {
    //private myPreview mPreview;

    Camera mCamera;
    byte[] data; 

    private void PictureCallback()
    {


    }

    private final class TakePhotoReceiver extends BroadcastReceiver {
        public final Camera mCamera;

        private TakePhotoReceiver(Camera mCamera) {

            this.mCamera = mCamera;

        }

        @Override
        public void onReceive(Context context, Intent intent) {

            try {// TODO Auto-generated method stub
            Log.v("test", "Got here");
            mCamera.takePicture(null, null, mPicture);
            //releaseCamera();
            } finally {
                releaseCamera();
            }
        }

        private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data2,Camera camera) {
                Log.v("test", "image taken");
                data = data2; 

                createExternalStoragePublicPicture(data);
                data = null;
                data2 = null;
                //releaseCamera();
            }
        };
    }



        }
        */

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    String mCurrentPhotoPath;
    void createExternalStoragePublicPicture(byte[] data) {

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + ".jpg";
        File image = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File file = new File(image, imageFileName);

        try {

            image.mkdirs();


            OutputStream os = new FileOutputStream(file);
            os.write(data);
            os.close();


            MediaScannerConnection.scanFile(this,
                    new String[] { file.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });
        } catch (IOException e) {

            Log.w("ExternalStorage", "Error writing " + file, e);
        }
        data = null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("Service Started");
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        try {
            //releaseCamera();
            final Camera mCamera = Camera.open(0);

            //myPreview mPreview = new myPreview(this, mCamera);
            //FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
            //    preview.addView(mPreview);
            this.registerReceiver(new TakePhotoReceiver(mCamera),
                    new IntentFilter(Intent.ACTION_SCREEN_ON));
                    //releaseCamera();
            //mCamera.stopPreview();
            //mCamera.stopPreview();




        } catch (Exception e) {
            Log.e(getString(R.string.app_name), "failed to open Camera");
            e.printStackTrace();
        }

        return START_STICKY;

    } 

    private void releaseCamera() {

        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }


    }

    private FrameLayout findViewById(int cameraPreview) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public void onDestroy() {
        System.out.println("Service Stopped");
        super.onDestroy();
        mCamera.release();
        //unregisterReceiver(this.TakePhotoReceiver);
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
    }

}
Was it helpful?

Solution

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Camera;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;


public class PictureService extends Service
{
    private static final String TAG = PictureService.class.getSimpleName();

    private TakePhotoReceiver mReceiver;



    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.d(TAG, "onCreate");
        mReceiver = new TakePhotoReceiver(0);
        registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        unregisterReceiver(mReceiver);
    }


    private final class TakePhotoReceiver extends BroadcastReceiver
        implements Camera.PictureCallback
    {
        public final int mCameraId;


        private TakePhotoReceiver(int cameraId)
        {
            mCameraId = cameraId;
        }

        @Override
        public void onReceive(Context context, Intent intent)
        {
            try{
                Camera camera = Camera.open(mCameraId);
                try{
                    Log.d(TAG, "onReceive: Taking picture");
                    camera.takePicture(null, null, this);
                }
                finally{
                    camera.release();
                    camera = null;
                }
            }
            catch(Exception e){
                Log.w(TAG, "onReceive: error with camera " + mCameraId, e);
            }
        }

        @Override
        public void onPictureTaken(byte[] data, Camera camera)
        {
            Log.d(TAG, "onPictureTaken");
            File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            savePictureTo(picturesDir, data);
        }
    }

    private void savePictureTo(File directory, byte[] data)
    {
        String fileName = new SimpleDateFormat("'JPEG_'yyyyMMdd_HHmmss'.jpg'")
            .format(System.currentTimeMillis());
        File file = new File(directory, fileName);
        try{
            directory.mkdirs();
            OutputStream os = new FileOutputStream(file);
            os.write(data);
            os.close();

            MediaScannerConnection.scanFile(this, new String[ ] { file.toString() }, 
                null, new MediaScannerConnection.OnScanCompletedListener()
            {
                @Override
                public void onScanCompleted(String path, Uri uri)
                {
                    Log.d(TAG, "savePictureTo: scanned path=" + path + ", uri=" + uri);
                }
            });
        }
        catch(IOException e){
            Log.w(TAG, "savePictureTo: error writing " + file, e);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top