Nexus 4 not showing files via MTP

2019-01-03 02:29发布

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?

12条回答
Explosion°爆炸
2楼-- · 2019-01-03 02:41

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

查看更多
Rolldiameter
3楼-- · 2019-01-03 02:41

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

查看更多
Summer. ? 凉城
4楼-- · 2019-01-03 02:43

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

查看更多
成全新的幸福
5楼-- · 2019-01-03 02:44

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.

  • Make sure that the device (Android) and the machine (where you want to copy the data) are reachable per IP or host name, for example by connecting them to the same wifi network.

Optional and recommended

  • Connect the android device using USB tethering. This ensures fast speed because of the wired connection and guarantees that both IP addresses are visible to each other because both are in the same network.

In the Android device

  • Install an SSH server. SSHelper worked like a charm
  • Run the SSHelper and go to the configuration tab to get the IP address, port, and password. In the example below, they are 192.168.1.5, 2222 and 'admin'.
  • The default password is admin but if you decide to change it hit the button "Restart server with new values" after changing it

In the machine

  • Install rsync. If happen to use Ubuntu it is already installed by default.
  • Open a shell and type the following command. Change the IP address and port to your values

rsync -avzhP --inplace --info=progress2 -e 'ssh -p 2222' 192.168.1.5:/storage/emulated/0/DCIM/Camera/ /home/username/path/to/pics/

  • you will get the following prompt, enter your password

SSHelper Version 8.5 Copyright 2014, P. Lutus Default password is "admin" (recommend: change it) username@192.168.1.5's password: admin receiving incremental file list

The other suggestions did not work for me:

  • Using USB storage mode instead of MTP is not supported by the device (as many other newer devices).
  • There was no option in my OS (Android 6.0.1) to clear the "Media Storage" app data.
  • Other methods of sharing files over the network had their draw backs. FTP does not ensure the files integrity. If the connection is dropped it either overwrites or skips all files, so one of the files might be incomplete. Other protocols might be hard to install and other apps might use proprietary protocols.
查看更多
\"骚年 ilove
6楼-- · 2019-01-03 02:49

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

查看更多
Summer. ? 凉城
7楼-- · 2019-01-03 02:49

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

查看更多
登录 后发表回答