Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't delete File with the File class

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)

like image 827
Ahmet Kazaman Avatar asked Jan 15 '16 13:01

Ahmet Kazaman


2 Answers

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.

like image 161
Minh Tri Nguyen Avatar answered Oct 15 '22 13:10

Minh Tri Nguyen


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

like image 44
Jared Rummler Avatar answered Oct 15 '22 14:10

Jared Rummler