0

I have used all of these functions:

  1. deleteFile(FileName);
  2. myFile.delete();

But none of these are deleting Files from sd card .I have tried my suggestions from stackoverflow but none of them are working. I have used the following code which appears to delete file from sd card but after some times the deleted files return back after some time. I donot know why .So plz help how to delete file from sd card.

 public static boolean delete(final Context context, final File file) {
    final String where = MediaStore.MediaColumns.DATA + "=?";
    final String[] selectionArgs = new String[] {
            file.getAbsolutePath()
    };
    final ContentResolver contentResolver = context.getContentResolver();
    final Uri filesUri = MediaStore.Files.getContentUri("external");

    contentResolver.delete(filesUri, where, selectionArgs);

    if (file.exists()) {

        contentResolver.delete(filesUri, where, selectionArgs);
    }
    return !file.exists();
}

I have used the following permissions

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

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Community
  • 1
  • 1
Dead King
  • 9
  • 4
  • have you tried this solution? [enter link description here](https://stackoverflow.com/a/24659789/6486268) – rmdan Jul 12 '18 at 12:05
  • Possible duplicate of [How do I delete files programmatically on Android?](https://stackoverflow.com/questions/24659704/how-do-i-delete-files-programmatically-on-android) – DamCx Jul 12 '18 at 12:05
  • What do you mean with "after a while"? Do they get deleted for a period of time? If so, the deleting part is working fine. It's something else that is creating the files again. I'm assuming you have all the required permissions in the manifest and request them to the user in your java code, right? – Luís Henriques Jul 12 '18 at 12:07
  • Did you check for permissions both in your manifest and in your java code? What version of Android are you using? – Luís Henriques Jul 12 '18 at 13:37
  • I have tried all solutions written above ,it worked well on files stored in internal storage but not on sd card – Dead King Jul 13 '18 at 10:53

0 Answers0