Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nexus 4 not showing files via MTP

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:

Screenshot

but Windows doesn't see it...:

Screenshot

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?

like image 408
moonlightcheese Avatar asked Dec 06 '12 05:12

moonlightcheese


3 Answers

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:

  • Clear the "Media Storage" app's data
  • Use the SDrescan or the SD Scanner (also works on Android 4.4/5, available on F-Droid) app to force an update

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

like image 132
Kevin Panko Avatar answered Nov 04 '22 04:11

Kevin Panko


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

like image 43
Sathesh Avatar answered Nov 04 '22 04:11

Sathesh


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);
like image 30
lviggiani Avatar answered Nov 04 '22 04:11

lviggiani