Reputation: 10590
I'm trying to simply write a simple XML file to the SD card and I noticed that my Nexus 4 does write the file, but it is not viewable via the MTP protocol using Windows 7.
code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CustomerQueryRqType customerQueryRequest = new CustomerQueryRqType();
Serializer serializer = new Persister();
File myFile = new File(Environment.getExternalStorageDirectory() + "/customerQueryRequest.xml");
try {
boolean created = myFile.createNewFile();
serializer.write(customerQueryRequest, myFile);
} catch (Exception e) {
e.printStackTrace();
}
}
I can see the file on the phone itself with Astro File Manager:
but Windows doesn't see it...:
adb shell
on the directory shows:
ls -l
drwxrwxr-x root sdcard_rw 1970-01-16 20:51 Alarms
drwxrwxr-x root sdcard_rw 1970-01-16 20:51 Android
drwxrwxr-x root sdcard_rw 2012-11-21 19:30 DCIM
drwxrwxr-x root sdcard_rw 1970-01-16 20:51 Download
drwxrwxr-x root sdcard_rw 1970-01-16 20:51 Movies
drwxrwxr-x root sdcard_rw 1970-01-16 20:51 Music
drwxrwxr-x root sdcard_rw 1970-01-16 20:51 Notifications
drwxrwxr-x root sdcard_rw 2012-11-19 12:06 Pictures
drwxrwxr-x root sdcard_rw 1970-01-16 20:51 Podcasts
drwxrwxr-x root sdcard_rw 2012-11-19 13:22 Ringtones
drwxrwxr-x root sdcard_rw 2012-11-19 14:33 bluetooth
-rw-rw-r-- root sdcard_rw 79 2012-12-05 22:26 customerQueryRequest.xml
drwxrwxr-x root sdcard_rw 2012-11-20 02:50 data
-rw-rw-r-- root sdcard_rw 11394 2012-11-19 13:54 eightpen_custom_gestures
drwxrwxr-x root sdcard_rw 2012-11-19 13:17 media
What's going on with my Nexus 4? Why is it hiding my things from Windows?
Upvotes: 80
Views: 68066
Reputation: 841
From the documentation here, you need to call MediaScannerConnection.scanFile :
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Upvotes: 0
Reputation: 919
The best workaround I found to the bug mentioned in the accepted answer is to copy data from Android OS to a another machine/server (Linux, MacOS, windows) using SSH.
Optional and recommended
In the Android device
In the machine
rsync -avzhP --inplace --info=progress2 -e 'ssh -p 2222' 192.168.1.5:/storage/emulated/0/DCIM/Camera/ /home/username/path/to/pics/
SSHelper Version 8.5 Copyright 2014, P. Lutus
Default password is "admin" (recommend: change it)
[email protected]'s password: admin
receiving incremental file list
The other suggestions did not work for me:
Upvotes: 4
Reputation: 1640
Had a similar issue with my OneplusOne:
After software update (to 6.0.1) i was not able to see my camera pictures when connecting to the computer (PTP and MTP). Both app's SDRescan and SDscanner had no effect.
My solution was to copy all pictures from DCIM to another folder using terminal or any file Explorer app on the Phone. When connecting to computer i was able to see the copied files.. i moved them to my computer and cleared my camera folder. No permanent solution but it fixed the problem for me at least until i do the next backup. =D
Upvotes: 1
Reputation: 8514
Seems to be a known issue affecting Android USB file access over MTP. The MTP cache gets out of date until a reboot of the phone.
A workaround is:
Or just avoid using USB at all. The issue does not affect other methods of accessing the files. For example, try AirDroid to transfer files.
Note: This doesn't work for Android 5.0.2: After clearing "Media Storage" and using an SD Rescanner, the folders appears in Windows 7 as unopenable 4K files. The only solution at that point is to clear once again Media Storage and restart the device
Upvotes: 72
Reputation: 2945
Recently I faced with the same problem but some additional research helped find hidden files. It seems that Google has made some changes in storage structure. All these files were moved to another folder This Computer\HTC One\Internal Storage\storage\emulated\0
. But it's strange that not all files were moved.
Android 4.4.2
Upvotes: 0
Reputation: 2181
I needed to create a directory and copy some sample files for my app. After a lot of experiments and research the following procedure worked flawlessly for me:
1) Created directory using File.mkdir().
2) Copied files in directory and run MediaScanner on each file.
MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
3) Run MediaScanner on directory.
MediaScannerConnection.scanFile(context, new String[]{directory.getAbsolutePath()}, null, null);
Result: The files and directory appear instantaneously on PC over MTP.
Upvotes: 1
Reputation: 6066
After spending hours on this issue I solved like this:
private void rescanFolder(String dest) {
// Scan files only (not folders);
File[] files = new File(dest).listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile();
}
});
String[] paths = new String[files.length];
for (int co=0; co< files.length; co++)
paths[co] = files[co].getAbsolutePath();
MediaScannerConnection.scanFile(activity, paths, null, null);
// and now recursively scan subfolders
files = new File(dest).listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
for (int co=0; co<files.length; co++)
rescanFolder(files[co].getAbsolutePath());
}
The thing is that you have to scan files only (not folders) and then repeat recursively for each fubfolder
EDIT
In addition, if you don't want pictures to be added to your photo album (but just your contents to appear over mtp protocol), remember to place an empty .nomedia
file in your folder before rescanning it, like this:
new File(myFolder + File.separator + ".nomedia").createNewFile();
rescanFolder(myFolder);
Upvotes: 5
Reputation: 49976
On Xperia Z below code worked just fine:
String screenShotFile = "....";
activity.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED, Uri
.fromFile(screenShotFile)));
for 4.4+ use:
activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(screenShotFile)));
(unfortunalelly I dont remember if I have tested it well enough)
http://commonsware.com/blog/2013/11/06/android-4p4-permission-regressions.html
Upvotes: 3
Reputation: 4940
The above answers are fine for creating the file. If you later delete the file and want that change to be reflected as well, you can do one of the following; let's first cover two solutions that seem like they work, but will cause problems on some devices. The first one is to scan the deleted file:
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
This works on many devices for deleted files, but on some devices it'll actually cause a new 0-byte file to be created, which probably isn't what you want. In that case, if you're running on < 4.4 KitKat, you can do this:
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(folder)));
Where folder is equal to file.getParentFile(). Unfortunately, on KitKat 4.4 and above, this will cause a SecurityException to get thrown, so for those devices, you can try deleting directly from the Media Store:
final Uri externalFilesUri = Files.getContentUri("external");
context.getContentResolver().delete(externalFilesUri, Files.FileColumns.DATA + "=?", new String[] {file.getAbsolutePath()});
I've tested this one as a workaround for ACTION_MEDIA_MOUNTED not working on KitKat, and it works in my testing on the Nexus 5.
Upvotes: 0
Reputation: 6428
You can use the following code after file close
MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, null);
Source: https://code.google.com/p/android/issues/detail?id=38282
Upvotes: 44
Reputation: 527
The simple code proposed to question How can I refresh MediaStore on Android? does the necessary update of MediaStore, if called after I put a file on external storage. Content on my Nexus 4 is refreshed instantly in Window 7 file explorer.
Upvotes: 2
Reputation: 29
notice /storage/emulated. Android 4.2 has some funky emulated per-app/user access stuff going on, for example a separate /sdcard/ for each user. Try moving the file to /sdcard/ instead of the emulated sdcard that is shown.
Upvotes: 2