androidcamera-api

Custom camera new captured image overwrites the old one


I have created a custom camera which snaps a picture and stores it in a folder in the internal storage of the phone. But whenever I snap a new picture, it over writes the old one.

MainActivity

public class MainActivity extends AppCompatActivity {
Camera camera;
Button capture, gallery;
FrameLayout frameLayout;
ShowCamera showCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frameLayout = findViewById(R.id.frameLayout);
    capture = findViewById(R.id.button);
    gallery = findViewById(R.id.button2);

    camera = Camera.open();
    showCamera = new ShowCamera(this,camera);
    frameLayout.addView(showCamera);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            captureImage(showCamera);
        }
    },4000);
}

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File picture_file = getOutputMediaFile();
            if(picture_file==null) {
                return;
            }
            try {
             FileOutputStream fos = new FileOutputStream(picture_file);
                fos.write(data);
                fos.close();
                camera.startPreview();
            }  catch (IOException e) {
                e.printStackTrace();
            }
        }

         };


private File getOutputMediaFile() {
    String state = Environment.getExternalStorageState();
    if(!state.equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    else {
        File folder_gui = new File (Environment.getExternalStorageDirectory() + File.separator+"GUI");
        if(!folder_gui.exists()) {
            folder_gui.mkdirs();
        }
        File outputFile = new File(folder_gui,"temp.jpg");
        return outputFile;
    }
}

public void captureImage(View v) {
    if(camera!=null) {
        camera.takePicture(null,null,mPictureCallback);
    }
}
}

ShowCamera

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback{

Camera camera;
SurfaceHolder holder;

public ShowCamera(Context context, Camera camera) {
    super(context);
    this.camera = camera;
    holder = getHolder();
    holder.addCallback(this);

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Camera.Parameters params = camera.getParameters();
    //change the orientation of the camera

    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size mSize = null;
    for(Camera.Size size : sizes) {
        mSize = size;
    }

    if(this.getResources().getConfiguration().orientation!= Configuration.ORIENTATION_LANDSCAPE) {
        params.set("orientation","portrait");
        camera.setDisplayOrientation(90);
        params.setRotation(90);
    }
    else {
        params.set("orientation","landscape");
        camera.setDisplayOrientation(0);
        params.setRotation(0);
    }
    params.setPictureSize(mSize.width,mSize.height);

    camera.setParameters(params);
    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

When a new picture is snapped, I want that picture to be added in the folder instead of overwriting the existing one. How do I do this?


Solution

  • Replace the code with this

        private File getOutputMediaFile() {
        String state = Environment.getExternalStorageState();
        if(!state.equals(Environment.MEDIA_MOUNTED)) {
            return null;
        }
        else {
            File folder_gui = new File (Environment.getExternalStorageDirectory() + File.separator+"GUI");
            if(!folder_gui.exists()) {
                folder_gui.mkdirs();
            }
            File outputFile = new File(folder_gui,System.currentTimeInMillies()+."jpg");
            return outputFile;
        }
    }
    

    Because you provide the same name at every time so it will replace your old file