10

Im trying to delete a music file through my App but can't achieve that. Ive checked with

boolean exists = temp.exists();
boolean isFile = temp.isFile();

if there true and yes they are. These methods returns me true. But when I come to the delete method :

boolean deleted = temp.delete();

It returns me False and the file is not getting deleted. There are no Exception throws just a false return to my deleted variable.

Im also using these permissons :

<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACTION_HEADSET_PLUG"/>

Someone got an Idea for a solution ? (Or other classes I can use ?)

Edit: Thats my full code

File temp = new File(str_path);

boolean exists = temp.exists();
boolean isFile = temp.isFile();

if (exists)) {
    boolean deleted = temp.delete();
    if (deleted) {
        Toast.makeText(context, "Successful deleted " + Title_Artist, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Not able to delete file " + Title_Artist, Toast.LENGTH_SHORT).show();
            }
        }

(And I checked while debuging if the object has his path in it and it have it)

Ahmet K
  • 713
  • 18
  • 42

4 Answers4

3

Delete file music you must do two task:

  1. Delete file in Storage.

    public static boolean delete(File path) {
        boolean result = true;
        if (path.exists()) {
            if (path.isDirectory()) {
                for (File child : path.listFiles()) {
                    result &= delete(child);
                }
                result &= path.delete(); // Delete empty directory.
            }
            if (path.isFile()) {
            result &= path.delete();
            }
            if (!result) {
                Log.e("Delete", "Delete failed;");
            }
            return result;
        } else {
            Log.e("Delete", "File does not exist.");
            return false;
        }
    }
    
  2. Delete file from MediaStore:

    public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
        int sdk = android.os.Build.VERSION.SDK_INT;
        if (sdk >= android.os.Build.VERSION_CODES.HONEYCOMB) {
           String canonicalPath;
           try {
               canonicalPath = file.getCanonicalPath();
           } catch (IOException e) {
               canonicalPath = file.getAbsolutePath();
           }
           final Uri uri = MediaStore.Files.getContentUri("external");
           final int result = contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath});
           if (result == 0) {
               final String absolutePath = file.getAbsolutePath();
               if (!absolutePath.equals(canonicalPath)) {
                contentResolver.delete(uri,
                        MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
                }
            }
        }
    }
    

You can reset/rescan MediaStore instead of do some code above.

Note: If you delete from SD card and android 4.4 +

Change for Android 4.4+ : Apps are not allowed to write (delete, modify ...) to external storage except to their package-specific directories.

  • But the problem I dont understand is how other apps do it ? Ive tried it out I cant delete a specific file from extern but the app Shuffler for example can delete it. – Ahmet K Jan 24 '16 at 14:10
2

The path from your comment looks like the file is on a removable SD card. You need special permissions on Android 4.4+ to manage or delete files on an SD card. You will need to use DocumentFile#delete().

For help accessing files on a removable SD card using DocumentFile see the following StackOverflow post:

How to use the new SD card access API presented for Android 5.0 (Lollipop)?


There is also a hack that might work without using DocumentFile as explained by the developer of FX file manager here: http://forum.xda-developers.com/showpost.php?p=52151865

Community
  • 1
  • 1
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
0

Since you are checking that the file exists, there can only be one reason you can not delete the file: you don't have permissions to do it.

An app can not delete system files, or files of other apps.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • But how are other Music Player apps doing it ? All the permissons I need I used in my manifest right ? – Ahmet K Jan 15 '16 at 13:43
  • Can you give me an example of what you are trying to delete? Just the paths of a few files. – Daniel Zolnai Jan 15 '16 at 13:47
  • /storage/extSdCard/Musik/neverlie_look.mp3 – Ahmet K Jan 15 '16 at 13:48
  • You can't edit files there, see this website: http://support.powerampapp.com/knowledgebase/articles/326024-can-t-edit-delete-files-on-sd-card-android-4-4 – Daniel Zolnai Jan 15 '16 at 13:51
  • 1
    I just tried it with the app Shuffle (Which is sth like poweramp) and it does work ? So they are able to delete it – Ahmet K Jan 15 '16 at 13:55
  • Am I supposed to move the file to my Phone storage and then delete it ? Or is it also not allowed to move files from extern Card – Ahmet K Jan 15 '16 at 18:13
0

Suppose your file path is

Environment.getExternalStorageDirectory().getPath()
                        + "/Music"
                        + "/"
                        + "song.mp3"

delete it like this

File dir = new File(Environment.getExternalStorageDirectory()
                        .getPath()
                        + "/Music");

if (dir.isDirectory()) {new File(dir, song.mp3).delete();}

if you want to delete all the files in music folder do this

if (dir.isDirectory()) {
                    String[] children = dir.list();
                    for (int i = 0; i < children.length; i++) {
                        new File(dir, children[i]).delete();
                    }
                }