当使用Android 5 Lollipop上的USB-MTP访问目录时,MediaScannerConnection#scanFile将目录转换为文件 [英] MediaScannerConnection#scanFile converts directories into files when accessing them with USB-MTP on Android 5 Lollipop
问题描述
诸如重命名应用触发的文件之类的更改仅在重新启动Android设备后出现在USB-MTP界面上,或者在MediaScanner上注册新文件后才出现在USB-MTP界面上,如下所示(请参见在特定路径(文件夹)上触发mediascanner,如何操作?):
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用于通过USB访问android设备的存储。例如。
USB-MTP is used to access the storage of an android device via USB. E.g. with the Windows Explorer.
,但是在Android 5.0.2(内部版本10.6.A.0.454)下使用Sony XPERIA Tablet Z(SGP321) ) newFile
中提供的文件夹将变为4KB的文件。我不再能够使用Windows资源管理器访问文件夹结构,也无法将文件复制到计算机上。即使重新启动平板电脑!与Android 4.4.4相同的设备不会显示此行为。看来只有USB-MTP视图损坏了。
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:
- 我当前的解决方法是避免目录使用
scanFile
。 - 我可以使用不带
MediaScannerConnection#scanFile
的android应用重命名文件,将文件转换回目录。重新启动后,我可以再次使用Windows资源管理器访问该目录。 - 使用Windows资源管理器重命名实际上是目录的文件不会还原它们。甚至在重新启动之后。
-
此行,如 https://stackoverflow.com中所建议/ a / 21918085/433718
不会刷新USB-MTP视图,但也不会将目录
转换为文件:
- 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
- 对各种文件操作使用内容解析器例如在此答案中删除文件: Android删除文件MediaScannerConnection
- 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)创建目录,但不要扫描目录
1) create directory, but don't "scan" directories
2)复制文件到目录
3)在文件路径上运行scanFile
3) run scanFile on the filePath
MediaScannerConnection.scanFile (_application, new String[] { filePath }, null, null);
这篇关于当使用Android 5 Lollipop上的USB-MTP访问目录时,MediaScannerConnection#scanFile将目录转换为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!