简体   繁体   English

如何仅将最后拍摄的图像保存到目录?

[英]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(). 我搜索了一下,发现可以用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. 编辑您可以列出目录中的文件mediaStorageDir List all the files of the directory, and delete the file which is older by comparing the last modified. 列出目录中的所有文件,并通过比较最后修改的文件来删除较旧的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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