0

Here I have created an app to take images and save them to external storage of the phone. (Also there is a problem with below code that images are not saved to the given location.) I want only the last taken image to be saved in external memory of the phone.Everytime I take a new picture, I need to delete the previously taken image and save only the last taken image. How can I do it? Also is it possible to take images continously at regular intervals? I searched and I found that I can do it with a Timer(). Is it possible? Thank You.

Edit- Actually what I want is to comapare two images. One is taken at the moment and other is taken immediately before it. (I take images at regular time intervals and I compare new one with the previous one.) Only after comparison, I delete previous one.

public class MyCamera extends Activity {
    private Camera mCamera;
    private CameraPreview mCameraPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mCamera = getCameraInstance();
        mCameraPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);


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

    /**
     * Helper method to access the camera returns null if it cannot get the
     * camera or does not exist
     *
     * @return
     */
    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    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.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, e.getMessage());
            }

        }
    };


    private static File getOutputMediaFile() {


        File mediaStorageDir = new File(
                Environment
                       .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "MyCameraApp");

        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        String fname = "IMG_" + timeStamp + ".jpg";
        System.out.println(fname);
        File mediaFile;
        mediaFile = new File(mediaStorageDir, fname);

        return mediaFile;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.my_camera, menu);
        return true;
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
HMD
  • 9
  • 6
  • Instead of deleting the last one, you can have a fixed name for the IMG as suggested below and just override it, the last one will be the current saved file. If you want to save more than one "last" picture you need to keep the picture on memory instead and probably offer a YES/NO screen to confirm the current. – Marcos Vasconcelos Feb 16 '18 at 17:59

1 Answers1

0

You can keep a constant name for your photo file.

  String fname = "MyImage.jpg";

You can give some constant name. And about taking image at regular interval, you can use handler. You can read more about it here. And make sure your remove your handler when your camera is closed.

EDITED You can list the files of your directory,mediaStorageDir in your case. List all the files of the directory, and delete the file which is older by comparing the last modified.

karandeep singh
  • 2,294
  • 1
  • 15
  • 22
  • I want only the last taken image to be saved in external memory of the phone.Everytime I take a new picture, I need to delete the previously taken image and save only the last taken image. Any solution for this? Thank You. – HMD Feb 16 '18 at 17:44
  • When you will give a constant name, previous image will be overwritten. – karandeep singh Feb 16 '18 at 17:45
  • Actuall what I want is to comapare two images. One is taken at the moment and other is taken immediately before it. (I take images at regular time intervals and I compare new one with the previous one. If any change occured, I need to send it to server to check for human detection) Phone is going to act as a cctv camera. But here they are images, not videos.( home monitoring system using smart phones.) – HMD Feb 16 '18 at 17:49
  • Only after comparison, I need to delete previous image. – HMD Feb 16 '18 at 17:53
  • You can list the files of the directory every time you take a picture, and delete the file which is older by comparing the lastModifiedData of the file. – karandeep singh Feb 16 '18 at 17:54
  • Thank You. And there is problem with my code. Images are not saved to directory. I still couldn't figure out what wrong I did. Can you please check my code? – HMD Feb 16 '18 at 17:58
  • Most probably you haven't added run time permissions. Please accept this as answer so that it can help others as well :) – karandeep singh Feb 16 '18 at 18:04
  • I can't upvote your answer, since I don't have enough reputation. :) – HMD Feb 16 '18 at 18:11
  • I have already added the permission. – HMD Feb 16 '18 at 18:23
  • You should try to debug your code and see if there are any logs – karandeep singh Feb 16 '18 at 18:25