6

My application does file operations on the SD card (delete or update file). But when I connect the device to Windows 7 through USB, I do not see any changes. However I can confirm files where updated or deleted with adb shell.

I use an ASUS eee Pad with Android 3.0.

It seems devices running Android 3.0 have a different way to provide access to the file system (MTP instead of USB Mass Storage). I also read about how we can use MediaScannerConnection.scanFile to refresh the content of Windows File Explorer when adding a new file.

But how to trigger a refresh when files are deleted or updated?

I tried MediaScannerConnection.scanFile on the deleted file but it only creates an entry with size 0. MediaScannerConnection.scanFile on the whole folder does not work either.

Thank you for your help.

G.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Georges
  • 203
  • 2
  • 9
  • Do you mean while the device is connected to USB? If so, then this might be intended behavior. – Tomislav Markovski Dec 21 '11 at 15:46
  • Unfortunately no. Even when the device is disconnected, I can see the deleted entries in Windows 7 File Explorer afterward. – Georges Dec 21 '11 at 15:57
  • 1
    I found an element of answer. sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); Doing this after a file was deleted seems to work when the device is not connected through USB. – Georges Dec 21 '11 at 16:21
  • Looks like a bug in the android platform. What you do is simply access the storage from android when it is (un)mounted which sort of commits the changes done to it. – Tomislav Markovski Dec 21 '11 at 16:24

1 Answers1

10

So, as mentioned in the comment before. For now the best I've found is

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

After a file was deleted or updated seems to work.

However, the device must be disconnected from USB. Otherwise you need to unplug and replug the device to see the changes.

NOTE: On some devices this may trigger a onResume event on the current activity.

Georges
  • 203
  • 2
  • 9
  • Congrats on the solution. When you are able, please mark you answer as 'accepted' so that others might learn from your success. Cheers~ – Andrew Kozak Dec 22 '11 at 16:34
  • I'm creating pdf files and always had to unplug and "re"-plug USB to see my changes... – stefan Jun 29 '14 at 22:11