2

I have this string,

Cursor c = db.obtenerDatoEjercicio(selecID);
String stringFoto1 = c.getString(6).toString();

then stringFoto1 is equals "file:///mnt/sdcard/Pictures/neoadnEditGyM/GYM_2.2.jpg"

This file exists.

I want to delete that file on the sd and I used the following code:

String stringFoto1 = "file:///mnt/sdcard/Pictures/neoadnEditGyM/GYM_2.2.jpg"
File archivo1 = new File(stringFoto1);
archivo1.delete();

But this doesn't work, please help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Juan A. Doval
  • 131
  • 2
  • 12

2 Answers2

4

You can try using this:

 String strFile = "/sdcard/Pictures/neoadnEditGyM/GYM_2.2.jpg"  
 File file = new File(strFile);
 boolean deleted = file.delete();

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

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

in AndroidManifest.xml file

Amilcar Andrade
  • 1,131
  • 9
  • 16
2

Finally, I used another method.

Because when I saved the photo was with the full path. I put only keep the name and well accessed by "enviroment".

 String folderSD = Environment.getExternalStorageDirectory().toString()+"/Pictures/neoadnEditGyM/";

        Cursor c = db.obtenerDatoEjercicio(selecID);
        String stringFoto1 = c.getString(6).toString();
        String stringFoto2 = c.getString(7).toString();
        File foto1 = new File(folderSD+stringFoto1);
        if (foto1.exists()){
            foto1.delete(); 
            Toast.makeText(this, stringFoto1+" deleted.", Toast.LENGTH_LONG).show();
        }
        File foto2 = new File (folderSD+stringFoto2);
        if(foto2.exists()){
            foto2.delete();
            Toast.makeText(this, stringFoto2+" deleted.", Toast.LENGTH_LONG).show();
        }
Juan A. Doval
  • 131
  • 2
  • 12