My app polls a directory on the SDcard for the appearance of new files that where dropped there by the user from Windows file explorer over the USB connection. When a new file appears, my app processes it, and then deletes it, however the file still shows up in Windows file explorer. I know the file is gone because it no longer appears in the DDMS file explorer, and my poller doesn't get triggered again. Any insights into how Android interacts with Windows file explorer would be appreciated. I've tried playing around with MediaScannerConnectionClient, which helps with getting programatically created directories to appear, but does nothing to get files to disappear.
I'm running Android 3.2 on an Acer Iconia A500. My PC is running Windows XP. The files are .csv and .txt files. I'm using File.delete() to delete them.
Thanks.
This is an old issue, but the above answers were not really helpful to me, so I tried some other stuff and the following worked for me.
Just call the scanFile method of the MediaScannerConnection on the file to be deleted after you delete it:
File file = new File("...");
String absolutePathToFile = file.getAbsolutePath();
file.delete();
MediaScannerConnection.scanFile(context, new String[]{absolutePathToFile}, null, null);
I am guessing that the scanner scans the file location, does not find the file and updates the OS's file index or whatever takes care of making the files visible to the explorer.
Android, being a Linux-based OS, will delete a file only when the last file handle to it is closed. The file name may disappear earlier, though.
On Windows, having an open file handle means the file name still exists. So Windows simply doesn't expect the file to disappear like it does.
If the files are gone when you refresh, it may just be because Windows doesn't expect the directory to change out from underneath it, and so doesn't check for changes.
Related
Changes like renaming a file triggered by an app only appear to the USB-MTP interface after reboot of the Android device or after you registered the new file at the MediaScanner them like this (see Trigger mediascanner on specific path (folder), how to?):
file.renameTo(newFile);
MediaScannerConnection.scanFile(context,
new String[] { newFile.getAbsolutePath() }, null, null);
USB-MTP is used to access the storage of an android device via USB. E.g. with the Windows Explorer.
However, with the Sony XPERIA Tablet Z (SGP321) under Android 5.0.2 (Build 10.6.A.0.454) folders supplied in newFile will become a file with 4KB. I am no more able to access the folder structure using Windows Explorer anymore, nor can I copy the file to my computer. Even after reboot of the tablet! The same device with Android 4.4.4 does not show the behavior. It appears that only the USB-MTP view is broken. The file structure accessed by an android app still looks fine.
Question: Is this behavior a bug or did I implement it incorrectly? What would be the correct implementation?
What I've tried so far to fix the issue:
My current workaround is to avoid scanFile for directories.
I can convert files back into directories by renaming them with an android app without MediaScannerConnection#scanFile. After reboot, I can access the directory with Windows Explorer again.
Renaming files with Windows Explorer that actually are directories does not restore them. Even after Reboot.
This line as suggested in https://stackoverflow.com/a/21918085/433718
does not refresh USB-MTP view, but also does not convert directories
into files:
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(newFile.getParentFile()));
Maybe related:
https://stackoverflow.com/a/27321544/433718
Using content resolver for all sorts of File operations like deleting a file in this answer: Android Deleting Files MediaScannerConnection
I ended up creating a dummy text file in each directory I wanted to make visible, and use scanFile on the file.
1) create directory, but don't "scan" directories
2) copy file to directory
3) run scanFile on the filePath
MediaScannerConnection.scanFile (_application, new String[] { filePath }, null, null);
I created a new Folder from within my app. I did so via
new File(folder-path).mkdirs();
The folder is called "Albums" and is located in /storage/emulated/0/Pictures.
On Android there are no problems, the folder appears in several filemanagers as it should. But when I connect the phone to my computer (Fedora 21), the Folder is just a binary file with filesize ~4kb, so I cant access the files inside. I checked Permissions on the folder but I think its okay.
What can I do?
Do I have to scan the file somehow to be recognized as a folder?
Im on Android 5.0.2
After creation (or after deletion) run:
MediaScannerConnection.scanFile(this,
new String[]{"/storage/emulated/0/Pictures/Albums/image1.jpg",
"/storage/emulated/0/Pictures/Albums/image2.jpg",
....}
, null, null}
Every file you change (create or delete) should be in the string array. Alternatively, you can restart your device every time you make a change. Note that this command does not work correctly on empty folders. It will show a 4 kb file instead, as you are seeing now.
I have an app where I want to be able to export to/import from CSV Files.
In a Use-Case I want to export data into a CSV File, connect the phone to a PC and edit the CSV there and re-import the file afterwards back into the app.
I am currently using a Motorola Razor I (latest android version) and no matter where I try to save the file, it does not appear to be public when connecting the phone to a computer (it's not there)
I can see the file on the android File Browser itself, so it is there, but in Windows Explorer the file is not there.
What would be a proper way to implement such a feature:
Export to CSV
Edit on Computer
Reimport same file
I've used function getExternalDirectory() and have also tried getExternalDirectoryPublic() - in either cases the file doesn't appear on the computer.
Any hints highly appreciated!
Ok, it seems, I missed something in order to make the file appear on the computer. Thing is, android treats the files not directly, but the MediaScanner does - so all I had to do is tell the MediaScanner about the new File and it works!
Writing the File with getExternalStoragePublicDirectory() and after the file is saved call
MediaScannerConnection.scanFile(MainActivity.this, new String[] { filename }, null, null);
to tell the MediaScanner about the newly file
Hopefully anyone find it useful
I have just made an application that uses a SQLite database, and I finally managed to export the database file onto the computer without having to root the Android device. The method I am using is mostly from code I found on various Stack Overflow question/answers. I am basically saving the database file to the public Download folder.
When I look on the Android device using a file explorer, I can see the correct SQLite file in the correct folder (Download folder). However, when I open up the same folder in Windows Explorer from my PC, I can not see the file in the exact same folder.
Now here is the strangest part: When I copy or move the file (using the Android device), even if I am copying and/or moving the file TO THE EXACT SAME SPOT, as soon as I have finished doing this the file instantly becomes visible in Windows Explorer.
I do not want the user to have to do this manually every time, so does anyone know of any way to make sure that the file will be visible right away? Thank you in advance!
Things I have tried so far:
Show hidden files and folders in Windows Explorer
Programmatically moving the file to a new folder within the application itself (the move to the new folder works, but the file still remains invisible in Windows Explorer until manually moving/copying the file again)
Searched StackOverflow for similar issues and have been unsuccessful.
And if anyone wants to see some of my code, let me know and I will post it, but I am thinking that this is less to do with my code and more to do with Android/Windows interaction. And this is where I got my move folder function from: stackoverflow.com/questions/4178168/how-to-programmatically-move-copy-and-delete-files-and-directories-on-sd
Thank you!
I had a similar issue and it was not visible in Windows because I hadn't given it a file extension.
Give it a file extension if you haven't already.
Through my Android program I wrote a file like this:
String file = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Files/hello.txt";
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(str+"\n"); \\yeah string has a value there
writer.close();
Now when I go to Android's "Astro" File browser, I can see the file hello.txt in /mnt/sdcard/Files but when I mount the sdcard in Windows, I can only see 4 other files which were there in that folder but not hello.txt.
In Windows itself, I even tried to uncheck the hide hidden files option just to ensure that the file is not hidden but it's just not visible.
I even tried to write the file in root of the sdcard but again same problem. Now I'm surprised that how is it possible that I can see the file in Android but not in Windows. In Android I've even checked the file contents and everything looks fine.
What could be the problem? Is the way I'm writing the file wrong?
PS: yes the Manifest permission android.permission.WRITE_EXTERNAL_STORAGE is added.
Ok, I figured out why it was happening.
Actually even if we press "Back" button, the program keeps running and unless I go to Settings > Applications > Manage Applications > "Force Stop" <application> I can't access the file written by this program even if it is on sd card and even if the filewriter has been closed.
This is just based on my observation and I'd like someone to post a better answer with facts and with solution.
edit: I'll update my question accordingly.
edit: as recommended on meta I've posted a new question
Can't see a file in Windows written by an android app on sd-card unless I “Force Close” the app
If worse comes to worst disconnect, reboot and reconnect your Android device. The files should show up then.
I have found a workaround.
I wrote a routine to capture data from the phone accelerometer. I noticed if the file had a .dat or .txt extension I could not see it and could not copy it to my PC.
By not assigning a file extension to the file on the Android side, I found it would be visible from the PC. After copying it to the PC and adding a .txt extension, it could then be viewed on the PC.
To display hidden files and folders
Open Folder Options in Control Panel
Click Start, and then click Control Panel.
Click Appearance and Themes, and then click Folder Options.
On the View tab, under Hidden files and folders, click Show hidden files and folders.