0

I am trying to delete an audio file in my app using the below code but for some reason it doesn't work out . file.delete() returns false even though the path is correct. Please help.I have also added WRITE and READ EXTERNAL STORAGE permission in Manifest. My phone is not connected to PC during testing.

    File file = new File(song.getPathId());

files.add(file);
  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);

        // set title
        alertDialogBuilder.setTitle("Delete");

        // set dialog message
        alertDialogBuilder
            .setMessage("Confirm delete")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    for(int i=0;i<files.size();i++)
                    {

                        fileDeleted=files.get(i).delete();//returns false but path is correct
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Siju
  • 2,585
  • 4
  • 29
  • 53
  • Please post an example of the path that `getPathId()` returns. – ashishduh May 19 '14 at 17:46
  • Path id :/storage/extSdCard/Music/baby.mp3 – Siju May 19 '14 at 17:51
  • Is file loaded in player? You can't delete file that is in use – jere_hr May 19 '14 at 18:18
  • The file is not playing when I try to delete it. – Siju May 19 '14 at 18:20
  • try like this ` if(files.get(i).exists()){ if(files.get(i).canWrite()){ fileDeleted=files.get(i).delete(); }else{ //file is locked... } }else{ //file not exists }` – jere_hr May 19 '14 at 18:28
  • Tried your approach . File exists and file write also true but still it didn't delete the file .Feeling annoyed :( – Siju May 19 '14 at 18:58
  • I know that feeling :) see if this can help you: [link](http://stackoverflow.com/questions/1248292/how-to-delete-a-file-from-sd-card) – jere_hr May 19 '14 at 19:24
  • Now tested on emulator. File is getting deleted. But on opening the app again its throwing a runtime exception at metadataretriever set data source . The file which was deleted is still setting the path. Damn confused. – Siju May 19 '14 at 19:30
  • On real device not deleting still. Tested on 2 real devices. – Siju May 19 '14 at 19:31
  • Able to delete from internal storage path : /storage/emulated/0/Ringtones/bbm_tone.wav . But unable to delete from External SD card . @jere_hr Can you suggest anything please? – Siju May 20 '14 at 14:16
  • I guess the issue is with Android 4.4.2 Kitkat since I am trying to delete a file which does not belong to app. Any suggestions how could it be done? – Siju May 20 '14 at 14:39
  • It is likely possible that this issue is only present for KitKat devices... see this: [link](http://www.androidcentral.com/kitkat-sdcard-changes) and this [link](http://stackoverflow.com/questions/22406061/howto-avoid-the-eacces-permission-denied-on-sdcard-with-kitkat-4-4-2-version) – jere_hr May 20 '14 at 17:24
  • But there should be a workaround since in other App like Samsung music player I am able to delete the file – Siju May 20 '14 at 17:27

0 Answers0