0

I have a file with this path:

file:/mnt/sdcard/Android/data/myapp/files/Pictures/IMG_20140108_160223.jpg

When I try this code:

File f = new File(path);
f.delete();

The file is not deleted. How can I do?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
fran
  • 1,119
  • 1
  • 10
  • 27

2 Answers2

1

You may need to use

file.getCanonicalFile().delete();

or even (assumming this is a context)

this.deleteFile("string");

More Infos here to delete file with context object

You may also delete your file:/ at the beginning of your File object creation.

Niklas
  • 23,674
  • 33
  • 131
  • 170
1

I've found a similar question here:

How to delete a file from SD card?

The file: prefix seems unnecessary.

try/mnt/sdcard/Android/data/myapp/files/Pictures/IMG_20140108_160223.jpg

Also you have to give permission if you are using >1.6 SDK

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

in AndroidManifest.xml file

Community
  • 1
  • 1
Jay
  • 601
  • 6
  • 17