I wrote a file through my Android program like this:
String file = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Files/hello.txt";
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(str + "\n"); \\ Yeah, 'str' has a value there
writer.close();
The program does its job and it finishes. Now I hit the back button on Android to close the application. If I then go to an Android file browser (like Astro) I can see the file, but if I mount the SD card on Windows, I can't see the file!
Now, if I go to Settings → Applications → Manage Applications → "Force Stop" <application>, I'm able to see the file even in Windows.
What should I do so that I can see a file in Windows if the file is written by my Android app on the SD card and I don't want to go to settings and hit force close every time.
Actually the file is being written properly (I think), but since the default behaviour of Android 'back' button doesn't kill the app, and it looks like unless the app is killed I can't open the file outside Android (in Android, I'm still able to see it and even open it).
So, what would be the best solution for this case? Should I automatically kill the app when it is closing? People say that System.exit() is strictly not recommended.
It´s necessary that you use the following code so that your archive will be shown in the file system of Windows or in the gallery of the phone.
You have two possibilities:
It´s faster because Android updates only the file passed:
MediaScannerConnection.scanFile(this, new String[]{"your path here</"}, null, null);
It´s more expensive because Android will search all the files in the path passed. You can pass only the path of the folder to reduce the search of the files:
context.sendBroadcast( new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + "path of the folder")));
Because a BufferedWriter object is a buffer, you need to flush() the buffer to force the content to be written to the output stream.
String file = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Files/hello.txt";
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(str+"\n");
writer.flush(); // force the buffer to actually dump its data to the stream
writer.close();
When you .write() using a buffer, always remember that the data will be held in the buffer until either the buffer is filled (and then automagically flush()ed) or you force the buffer to flush its data using the .flush() method of the buffer.
Additionally, the reason the file wasn't being written to correctly until you force-closed the application is because when you kill an application, one of the steps taken is to flush() all buffers associated with the application. That's why this was only happening when you force-killed it.
Note: Bear in mind that this is the default behavior of most kinds of buffered streams in many different languages.
Windows 7 did have issues seeing files when connected through the USB connection. The workaround for me was using a GNU/Linux distribution.
Using a terminal app, there is a difference where root:root
files are viewable on Explorer, but no other owners.
来源:https://stackoverflow.com/questions/7429087/cant-see-a-file-in-windows-written-by-an-android-app-on-sd-card-unless-i-force