Why does MTP show wrong directory location?


Question

MTP (Miserable Transfer Protocol) failed once again.



After moving a directory to a different path, it is still on the same path as before according to MTP.



I already tried re-plugging the USB port and reading it from a different computer. Same result.



Why is MTP so bogus that it shows a moved folder on its previous path?


Answer

What is Content Provider?



It seems to be a problem related to Media Providers. As the name suggests, they provide a database of available media in a more organized way to other apps as the Contacts Storage (com.android.providers.contacts) provides contacts to requesting apps. Purpose is to scan and store media file info for quick access so that individual apps don't have to scan folders manually for an up-to-date library, reducing time and effort.



Android's built-in Media Providers:



Some file explorers, media players and gallery apps depend on the files database (/data/data/com.android.providers.media/databases/external.db) built by Media Storage app for MediaStore content provider from the contents of external storage (/sdcard and external SD card; removable storage like USB isn't indexed). Apps Media Storage (com.android.providers.media under which com.process.media runs), Downloads (com.android.providers.downloads.ui), Download Manager (com.android.providers.downloads) and MTP Host (com.android.mtp) work in collaboration under same UID.



Database is built/updated as MediaScanner (android.process.media) is invoked by some app such as by sending broadcast MEDIA_SCANNER_SCAN_FILE or through scanFile. Broadcast receiver com.android.providers.media.MediaScannerReceiver is launched on boot that listens for android.intent.action.BOOT_COMPLETED, so MediaScannerService will also launch on boot. But restarting phone sometimes doesn't update whole external database (or at least delays). MediaScanner is also triggered when a new storage device is mounted i.e. on MEDIA_MOUNTED which could possibly be manually broadcasted to force updation on pre KitKat versions.

In response, apps are informed of updated databases such as by receiving broadcast MEDIA_SCANNER_FINISHED or through onScanCompleted.



Problem with Media Providers:



MtpService is also a part of Media Storage. Due to some bugs (acknowledged since 2012) in implementation of MTP, database isn't updated or is delayed or isn't properly communicated with the client as the files change, so MTP shows outdated / incomplete contents. The same can happen if an app creating or downloading a new file fails to trigger the MediaScanner or fails to receive back the updated information.



Solution:



In order to force database updation, delete the file and run some media scanner app. Non-root users can delete this file by clearing data of Media Storage app.

But be aware, it'll also clear internal.db which will make ringtones unavailable and a new database will be built from /system/media on next reboot i.e. on BOOT_COMPLETED.



PS: Note that the internal SD card we see is emulated from /data/media/ using sdcardfs (or FUSE in older versions of Android). Actual contents of internal memory can be seen directly from /data/media/ (root required). UMS used to expose the /data block device to PC but was deprecated in favor of MTP. It's a good option when larger size of data is to be transferred at high speeds. It can be enabled by some workaround.

There are also other reasonable alternatives for MTP like FTP, SFTP/SSHFS, adbfs etc.



RESOURCES:




Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes