0

I've seen a lot of posts here that say that this is solved, but watching at the dates, I think the most recent I've seen is from 2015. I've tried what I found and couldn't solve it.

Mainly, what I've tried was to find the file (in different ways) from the uri, but it says it doesn't exists.

Also I've found this:

getContentResolver().delete(data.getData, null, null)

called inside the onActivityResult(int requestCode, int resultCode, Intent data) method, after I've taken the picture from the camera. In this case I got a NPE because data comes as null.

EDIT

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_open_camera) {
        ContentValues values = new ContentValues();
        imageName = "Example" + System.currentTimeMillis();
        values.put(MediaStore.Images.Media.TITLE, imageName);
        values.put(MediaStore.Images.Media.DESCRPITION, "Camera_" + System.currentTimeMillis());
        imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePictureIntent.putExtra(MediaStore.EXTRA_MEDIA_TITLE, imageName);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageuri);
        if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, 1);
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT.OK) {
        Bitmap image = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
        // I do some stuff with this image here and want to delete it afterwards
        File fDelete = new File(imageUri.getPath());
        getApplicationContext().deleteFile(fDelete.getName());
    }
}
Silkking
  • 250
  • 3
  • 15
  • Please edit your question and provide a [mcve]. This would include the code where you have "taken the picture from the camera" and the implementation of your `onActivityResult()` method. – CommonsWare Mar 12 '18 at 13:44
  • The documentation of deleteFile in class context states: "Delete the given private file associated with this Context's application package." if you adding to external storage I think it shouldnt work. Try the defualt file.delete() – Marcos Vasconcelos Mar 12 '18 at 14:34
  • @MarcosVasconcelos, I've tried that before and now again, with no success. – Silkking Mar 12 '18 at 14:50
  • `File fDelete = new File(imageUri.getPath());` That will not give you a file system path. Please tell the value of `imageUri.getPath()`. Also tell the value of `imageUri.toString()`. – greenapps Mar 12 '18 at 15:45
  • imageUri has a schema and a path, and will probably not be file://, you will need to delete it getting from the content resolver again (I dont remmeber so i'll not post as an answer) – Marcos Vasconcelos Mar 12 '18 at 15:50
  • @greenapps, imageUri.toString gets "content://media/external/images/media/47639" and imageUri.getPath gets "/external/images/media/47639" – Silkking Mar 12 '18 at 16:48
  • @MarcosVasconcelos, I think what you are saying is the same as I've put up there – Silkking Mar 12 '18 at 16:49
  • So indeed that is not a file system path. You could have confirmed that. – greenapps Mar 12 '18 at 17:25
  • You use the content resolver to delete the file. Ok. But most content resolvers do not implement this option. – greenapps Mar 12 '18 at 17:26

1 Answers1

0

If you want to delete a picture you let take by the camera app then do not use a content uri from the mediastore to begin with.

Just build up a file system path and use a file provider and put the obtained uri in EXTRA_OUTPUT.

When done use your file system path to delete the file.

greenapps
  • 11,154
  • 2
  • 16
  • 19