0

I have Uri image which I store from ActivityOnResult. I would like to remove this image few steps later.

File file=new File(uri.toString());
//I tried file.mkdir() too, but without change
if(file.exists())
     file.delete();

But file.exists() return false. I have no idea what can be the reason. I will be gratefull for any tipis

pupilx
  • 75
  • 1
  • 2
  • 10
  • 1
    There is no way to answer your question as is. The only answer we could give is that the File simply *does not exist*. What is the URI? – Kevin Coppock Aug 06 '14 at 20:01

5 Answers5

2

It's a content:// URI meaning you can't treat it as a normal file. Instead try this. getContentResolver().delete(uri, null, null);

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
Simon
  • 10,932
  • 50
  • 49
0

Try this

File file=new File(uri.getPath());
//I tried file.mkdir() too, but without change
if(file.exists())
     file.delete();
Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50
  • Can you log what `uri.getPath()` is returning? Where the image is located and do you have permission to write? You can check that with `file.canRead()` – Gokhan Arik Aug 06 '14 at 19:56
  • Yes, I have permission in manifest. Image is located content://media/external/images/media/84 and getPAth() return /external/images/media/84 – pupilx Aug 06 '14 at 20:00
  • 1
    That is not a file path. That's a content provider URI. `/external/images/media/84` is not a file and therefore, does not exist. – Kevin Coppock Aug 06 '14 at 20:02
  • Check that solution - http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore?rq=1 – Gokhan Arik Aug 06 '14 at 20:06
0

Do you know what is the value in uri? toString should work, getPath will return the decoded path which might not be valid.

Amrut
  • 543
  • 4
  • 8
0

You need to check exists on getAbsoluteFile().

Try this

 File file=new File(uri.toString());
//I tried file.mkdir() too, but without change
 if(file.getAbsoluteFile().exists()) {
   file.delete();
 }
AnkitSomani
  • 1,152
  • 8
  • 9
0

The uri whihc you are getting in onActivityResult might be content scheme uri..Please print the uri.Toi convert content scheme uri to file uri refer the following SO question:

Get filename and path from URI from mediastore

Community
  • 1
  • 1
rupesh jain
  • 3,410
  • 1
  • 14
  • 22