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)
Delete file music you must do two task:
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;
}
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With