0

How to update the file system of SD-card? Windows explorer (and the other programs which open file system) crashes. It shows all files new and deleted.

The method below just updates existing files:

MediaScannerConnection.scanFile (this, new String[] {file.toString()}, null, null);

As I understood below method works just for Android < 4.4 version:

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

How to update deleted files and folders?


EDITED:

CODE EXAMPLE (creating and removing file):

//i'm createing the file:
//in works properly!
File file = new File(dir, "filename.pos");
os = new DataOutputStream(new FileOutputStream(file));
os.writeLong(versionOfPOSFile);
os.close();
MediaScannerConnection.scanFile (context, new String[] {file.toString()}, null, null);



//this code deletes a file:
//it doesn't work!
//files are not updated in Windows.
File path = /*some folder path*/
File[] c = path.listFiles();
 for (File file : c){
file.delete();
//even I add this code:
MediaScannerConnection.scanFile(context, new String[] {file.toString()}, null, null);
}
}
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • If windows explorer crashes then how can it show all files? – greenapps Jul 01 '14 at 07:15
  • @greenapps I shows files at first. When I try to open/copy - explorers crashes. – Vyacheslav Jul 01 '14 at 07:29
  • You could have told that before. But explorer can remove files and not crash? As then on!y refreshing fails? – greenapps Jul 01 '14 at 07:34
  • No. It shows the files in his window. But when I try to do something with this files the application doesn't respond. – Vyacheslav Jul 01 '14 at 08:12
  • You told that it crashed. And now it does not respond? Why not tell better what is all happening? Which protocol is used? MTP? You should really tell more about the setup. – greenapps Jul 01 '14 at 08:28
  • Phone: Samsung galaxy s3. Protocol MTP. The app creates some files in the folder on SD Card, several removes. If I try to look new files on SD Card in Explorer Windows I see all the files (deleted and new). If refresh the folder in Explorer - nothing happens. If I try to copy new files from file system - they are copyed. If I try to remove files already removed from SD Card Explorer at first time doesnt respond, than crashes in several seconds (may be one minute). – Vyacheslav Jul 01 '14 at 09:03
  • Does your app after it created a new file call the media scanner for it? Your app is not creating and deleting files during MTP connection with WIndows is it? As then Windows will not be informed right away. – greenapps Jul 01 '14 at 09:24
  • Yes. The app calls MediaScanner. by the method I wrote in the topic. Look more at the bottom of topic( I've added some code) – Vyacheslav Jul 01 '14 at 10:16
  • One question you did not answer though. Did you search/google for your problem? Did you read: http://stackoverflow.com/questions/8379690/androids-media-scanner-how-do-i-remove-files – greenapps Jul 01 '14 at 11:07
  • RemoveAllForPaths function you send doesnt work. I have no MTP connection wthin my app. What do you mean about " MTP connection with WIndows"? I've tryed different ways to fix. your link I tried too. – Vyacheslav Jul 01 '14 at 11:13
  • `One question you did not answer though`: `Your app is not creating and deleting files during MTP connection with WIndows is it?` Please do answer. You confirmed that your Android device and Windows were communicating using MTP. The Media Transfer Protocol. – greenapps Jul 01 '14 at 11:22
  • No I did nothing to set this communication. How to do this? – Vyacheslav Jul 01 '14 at 12:06
  • No? I did not say you had to set/configure it. You confirmed earlier that MTP was used. If it is working now then you do not have to set it anymore. But you still did not answer my question. – greenapps Jul 01 '14 at 12:11
  • Excuse me, but I do not understand you. How to establish the MTP communication? What do you mean? Is it automatic connection in OS Windows when I plug-in my Android device ? Or I have to write some code to establish? – Vyacheslav Jul 01 '14 at 14:02
  • As said before: if everything works then you don't have anything to do anymore. Nothing. Only answer my question: `Your app is not creating and deleting files during MTP connection with WIndows is it?` Refrased: Is your app creating or deleting files while your phone is connected -by an usb cable- to your computer? – greenapps Jul 01 '14 at 14:16
  • Yes. Of course. Phone is in USB. Before executing the code - there is a file (which will be removed). After executing in windows explorer - 2 files - a new created and an old (which removed and in any file manager in Android this file is excluded). – Vyacheslav Jul 01 '14 at 16:32
  • Well i can not follow your explanation sorry. I dont understand what you are doing. But i was already afraid that you were experimenting while the phone was connected to your pc. Well do not do that. You are asking for troubles. – greenapps Jul 01 '14 at 16:45

0 Answers0