2

I'm trying to delete some files or directories from the internal memory (ASUS TF101) using the commands File.delete(). However, using the Android File explorer I can see how the files are removed, but using the Windows explorer they still show there.

I solved a similar issue when adding files just creating a MediaScannerConection (and using the method scanFile).

Does anyone know how to solve this problem? Of course it´s not acceptable to restart the device as the final user will not like this "solution".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1572758
  • 21
  • 1
  • 3

2 Answers2

3

See the answer of manisha in this thread: Android file delete leaves empty placeholder in Gallery

In my case, it worked fine because i was saving a photo and then scanning it using the MediaScannerConnection, so when the scan finishes i have the URI of that file and then i can use the following code whitout problems.

getContentResolver().delete(imageUri, null, null);

Hope this works for you. This seems to be more suitable than forcing a SD Card remount by sending an intent to android system.

Community
  • 1
  • 1
Luis
  • 709
  • 8
  • 10
  • Thanks a lot man.. I have tried so many methods but none of them worked in android 4.2 but your suggestion worked fine.. – Praveena Feb 13 '15 at 15:01
0

I had the same problem, but I found the answear:

Android uses a media table to handle files. When you make changes to a file, you must update this table.

Try this:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Google doc: http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_MOUNTED

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • This no longer work since Android 4.4 KitKat, reference https://android.stackexchange.com/a/57915 – VCD Jan 03 '18 at 01:05