What is Mediaserver?
Mediaserver or Media scanner on Android is designed to scan and index every media file: Images, Videos, Music and make the list available to all Android apps, so that they don’t have to do the repetitive tasks. But it isn’t perfect.
Media server can have strange behaviors and tends to be buggy. Very often you can end-up shedding 50% of your battery on Media scans. Not only does media scanner go wrong on custom ROMs but many of the official stock ROMs too. So out of the blue, one day the media scanner would run crazy and drain your battery.
How to Fix Mediaserver / scanner Battery drain
One quick way to get around is to stop Media scanner service altogether (using apps on Play store) but that way your songs, photos, videos would vanish from your gallery, music player.
The situation is more frequent once your sdcard (internal or external) gets old and you’ve lots of files.
I had been digging out how to fix it, and here are my solutions. Perhaps try al of them, it would definitely relieve your cpu and battery usage by large. Solution works on any Android version:
Solution 1. Clear media storage data
The simplest solution is to clear media storage database and let it restart the process from scratch. Chances are this will fix most of the issues with Mediascanner/Mediaserver. Do the following, and it should help ease-out the pain:
Settings > applications > Media Storage > clear data, force stop and reboot.
Solution 2. Remove unwanted media files
Mediaserver really does a hard job while analyzing each media file it encounters, reading metadata, generating thumbnails, inserting into Android mediastore database. You can ease-out its processing by removing unwanted, trash files.
Navigate to /sdcard/DCIM/.thumbnails and delete all files. Repeat for external sd card. On AOSP ROMs, Android gallery creates too many of thumbnails that later becomes problematic for media scanner. Deleting them once in few months is a good idea.
Note: If this is a very slow process, use adb commandline to delete these
Solution 3. Analyze and Delete excessive media files
As we discussed in solution 2 that mediaserver can be doing heavy lifting due to excessive media files on sdcards. In this step we find-out what files are causing media scanner to go mad.
You can use any sdcard analyst from Play Store (I use ES file manager > menu > Sd card analyst) to determine which directory has lots of files/subdirectories. Any directory having >1000 files/folders is an alarmingly high number. Get rid of them (if you can). However, do not delete any apps/system files in the process.
Solution 4. Probe and Delete bad/damaged media
If you’re sure you’ve tried above solutions and still encountering battery drain, its time to dig deeper with the help of Android developer tools.
- You must be rooted,
- Android developer tools setup with ADB being able to detect devices connected to USB (Windows, OS X, or Linux)
With ADB tools installed, enter shell and acquire SuperUser permissions:
Note: You might be prompted on phone to authorize superuser access. You must Grant it
Then run “top” (linux process manager) to see which process is eating your phone’s CPU. The command below filters top results related with media scanner:
top -grep media
Running the above command would respond something like:
130|root@n7000:/ # top | grep media
1905 0 15% S 7 23080K 6168K bg media /system/bin/mediaserver
2808 0 42% S 3 3524K 740K media_rw /system/bin/sdcard
2825 0 11% S 19 255832K 43936K bg u0_a5 android.process.media
%age cpu shows media server is eating cpu and needs to be fixed. Run the following command to determine which file is it currently reading.
lsof | grep media_rw
root@n7000:/ # lsof | grep media_rw
sdcard 2808 media_rw exe ??? ??? ??? ??? /system/bin/sdcard
sdcard 2808 media_rw 0 ??? ??? ??? ??? /dev/null
sdcard 2808 media_rw 1 ??? ??? ??? ??? /dev/null
sdcard 2808 media_rw 2 ??? ??? ??? ??? /dev/null
sdcard 2808 media_rw 3 ??? ??? ??? ??? /dev/fuse
sdcard 2808 media_rw 4 ??? ??? ??? ??? anon_inode:inotify
sdcard 2808 media_rw 5 ??? ??? ??? ??? /mnt/media_rw/sdcard1
sdcard 2808 media_rw 6 ??? ??? ??? ??? /mnt/media_rw/sdcard1/audiobooks
sdcard 2808 media_rw 7 ??? ??? ??? ??? /mnt/media_rw/sdcard1/audiobooks/2-14 Einstein CD 02- Track 14.mp3
If the media service is stuck at a directory/file for a long time, you know it’s a damaged media file, you should probably get rid of it.
Once you get rid of all such files/directories, its guaranteed media server would finish scanning soon enough.
Solution 5: Format SD cards
If nothing works (or you’re noob enough to try solution 4): Take backup and format Internal, external SD card and copy only selective files/folders that you need. Problems would go away for sure.
Solution 6: Disable Media scanner
If your Droid is against you and it just won’t tame, its time to disable the media scanner. You can do this from adb shell
pm disable com.android.providers.media/com.android.providers.media.MediaScannerReceiver
You can re-enable it later with:
pm enable com.android.providers.media/com.android.providers.media.MediaScannerReceiver
Say goodbye to mediascanner problems, forever.
Still got problems? Leave your logs in the comments section, we will get back to you as early as possible.