4

I have used manual on http://developer.android.com/guide/topics/media/camera.html but I have a problem with it. This code save image twice. First picture is saved in /sdcard/DCIM/Camera and the second picture is saved in /sdcard/PicturesMyCameraApp. How can I remove the first saving to /sdcard/DCIM/Camera. Thanks for help

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
        case R.id.imageButton1:
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
            startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        break;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
        case R.id.item1:
            /*Intent intent = new Intent(ZodiacActivity.this, AboutActivity.class);
            startActivity(intent);*/
        break;
        case R.id.item2:
            this.moveTaskToBack(true);
        break;
    }
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);  

    if (requestCode==CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode==RESULT_OK) {
            Toast.makeText(this, "Uloženo do: " + getOutputMediaFileUri().toString(), Toast.LENGTH_LONG).show();
        }
        else {
            return;
        }
    }
}

public Uri getOutputMediaFileUri(){
      return Uri.fromFile(getOutputMediaFile());
}

public File getOutputMediaFile(){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        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());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}
Sexy Meda
  • 95
  • 1
  • 10

1 Answers1

0

You can simply delete the file:

String pathToFile = "/sdcard/DCIM/Camera/imagename.jpg";
boolean success = new File(pathToFile).delete();

if(success){
// deleting file succeeded
}else{
// it didn't succeed
}

Edit

Get the latest taken picture in /sdcard/DCIM/Camera:

File[] images = new File("/sdcard/DCIM/Camera").listFiles();
File latestSavedImage = images[0];
for(int i=1; i<images.length; ++i){
 if(images[i].lastModified() > latestSavedImage.lastModified()){
   latestSavedImage = image;
 }
}

Note, the code was not tested!

poitroae
  • 21,129
  • 10
  • 63
  • 81
  • 1
    The problem is that every two pictures have different timeStamp of name. First picture is saved to /sdcard/PicturesMyCameraApp and second /sdcard/DCIM/Camera and have different name... So for example first picture has name 165337 (format hhmmss) and second one has 165343 and so on. – Sexy Meda Dec 19 '11 at 15:56
  • 2
    @user1106177 I think there's a fundamental problem there. If you want to create a CUSTOM camera app, it's the wrong approach to start a 3rd party camera app. And what a 3rd party app most likely does is saving the created picture to /DCIM/Camera. How to create a 'real' custom camera, see here: http://developer.android.com/reference/android/hardware/Camera.html Because a 3rd party app is saving the picture, you cannot say what it name will be, as you mentioned. The only thing you can do is iterate through all images in /DCIM/Camera, find the one with the latest timestamp, and delete this one. – poitroae Dec 19 '11 at 15:58