简体   繁体   中英

How can I save only the last taken image to directory?

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;
    }

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM