[Galaxy S II Android Development] shoman94:
As you all know a lot of people have a media scanner wakelock. It's a well known issue across all Google JB Phones. Google has implemented a fix for 4.2 which seems to fix it but there are still things that should be done by users.

Googles FIX: https://android.googlesource.com/platform/frameworks/base/+/8ab2dc2f9680307febe997631c2148729f714e3d

Seems the issue stems from corrupt thumbnail or other files. Also the issue is expanded by SQL Wildcard such as underscore [ _ ] in the file name.

Also note that .nomedia in the folders don't seem to work anymore as posted by Koushik Dutta (Koush) CWM: https://plus.google.com/u/0/103583939320326217147/posts/Ntgmq2oniA5

So while looking at the fix it seems that the CM team has implemented Google's fix in there source here: http://review.cyanogenmod.org/#/c/27862/

So we decide to try this in our ROM and it seems from my test users with the issue that it has fixed it so we want to share what needs to be changed so you can mod your ROM yourself or Devs to add this to their ROM.

First up is to Decompile Framework.jar

Go to:

and find:

.method makeEntryFor(Ljava/lang/String;)Landroid/media/MediaScanner$FileEntry;

Replace the whole method with this:

.method makeEntryFor(Ljava/lang/String;)Landroid/media/MediaScanner$FileEntry;
.locals 14
.parameter "path"

.prologue
.line 1470
const/4 v12, 0x0

.line 1472
.local v12, c:Landroid/database/Cursor;
:try_start_0
const-string v0, "_"

invoke-virtual {p1, v0}, Ljava/lang/String;->contains(Ljava/lang/CharSequence;)Z

move-result v0

if-nez v0, :cond_0

const-string v0, "%"

invoke-virtual {p1, v0}, Ljava/lang/String;->contains(Ljava/lang/CharSequence;)Z

move-result v0

if-eqz v0, :cond_4

:cond_0
const/4 v13, 0x1

.line 1474
.local v13, hasWildCards:Z
:goto_0
if-nez v13, :cond_1

iget-boolean v0, p0, Landroid/media/MediaScanner;->mCaseInsensitivePaths:Z

if-nez v0, :cond_5

.line 1479
:cond_1
const-string v3, "_data=?"

.line 1480
.local v3, where:Ljava/lang/String;
const/4 v0, 0x1

new-array v4, v0, [Ljava/lang/String;

const/4 v0, 0x0

aput-object p1, v4, v0

.line 1487
.local v4, selectionArgs:[Ljava/lang/String;
:goto_1
iget-object v0, p0, Landroid/media/MediaScanner;->mMediaProvider:Landroid/content/IContentProvider;

iget-object v1, p0, Landroid/media/MediaScanner;->mFilesUri:Landroid/net/Uri;

sget-object v2, Landroid/media/MediaScanner;->FILES_PRESCAN_PROJECTION:[Ljava/lang/String;

const/4 v5, 0x0

const/4 v6, 0x0

invoke-interface/range {v0 .. v6}, Landroid/content/IContentProvider;->query(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Landroid/os/ICancellationSignal;)Landroid/database/Cursor;

move-result-object v12

.line 1489
invoke-interface {v12}, Landroid/database/Cursor;->moveToFirst()Z

move-result v0

if-nez v0, :cond_2

if-eqz v13, :cond_2

iget-boolean v0, p0, Landroid/media/MediaScanner;->mCaseInsensitivePaths:Z

if-eqz v0, :cond_2

.line 1494
const-string v3, "_data LIKE ?1 AND lower(_data)=lower(?1)"

.line 1495
const/4 v0, 0x1

new-array v4, v0, [Ljava/lang/String;

.end local v4 #selectionArgs:[Ljava/lang/String;
const/4 v0, 0x0

aput-object p1, v4, v0

.line 1496
.restart local v4 #selectionArgs:[Ljava/lang/String;
invoke-interface {v12}, Landroid/database/Cursor;->close()V

.line 1497
iget-object v0, p0, Landroid/media/MediaScanner;->mMediaProvider:Landroid/content/IContentProvider;

iget-object v1, p0, Landroid/media/MediaScanner;->mFilesUri:Landroid/net/Uri;

sget-object v2, Landroid/media/MediaScanner;->FILES_PRESCAN_PROJECTION:[Ljava/lang/String;

const/4 v5, 0x0

const/4 v6, 0x0

invoke-interface/range {v0 .. v6}, Landroid/content/IContentProvider;->query(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Landroid/os/ICancellationSignal;)Landroid/database/Cursor;

move-result-object v12

.line 1502
:cond_2
invoke-interface {v12}, Landroid/database/Cursor;->moveToFirst()Z

move-result v0

if-eqz v0, :cond_8

.line 1503
const/4 v0, 0x0

invoke-interface {v12, v0}, Landroid/database/Cursor;->getLong(I)J

move-result-wide v6

.line 1504
.local v6, rowId:J
const/4 v0, 0x2

invoke-interface {v12, v0}, Landroid/database/Cursor;->getInt(I)I

move-result v11

.line 1505
.local v11, format:I
const/4 v0, 0x3

invoke-interface {v12, v0}, Landroid/database/Cursor;->getLong(I)J

move-result-wide v9

.line 1506
.local v9, lastModified:J
new-instance v5, Landroid/media/MediaScanner$FileEntry;

move-object v8, p1

invoke-direct/range {v5 .. v11}, Landroid/media/MediaScanner$FileEntry;-><init>(JLjava/lang/String;JI)V
:try_end_0
.catchall {:try_start_0 .. :try_end_0} :catchall_0
.catch Landroid/os/RemoteException; {:try_start_0 .. :try_end_0} :catch_0

.line 1510
if-eqz v12, :cond_3

.line 1511
invoke-interface {v12}, Landroid/database/Cursor;->close()V

.line 1514
.end local v3 #where:Ljava/lang/String;
.end local v4 #selectionArgs:[Ljava/lang/String;
.end local v6 #rowId:J
.end local v9 #lastModified:J
.end local v11 #format:I
.end local v13 #hasWildCards:Z
:cond_3
:goto_2
return-object v5

.line 1472
:cond_4
const/4 v13, 0x0

goto :goto_0

.line 1484
.restart local v13 #hasWildCards:Z
:cond_5
:try_start_1
const-string v3, "_data LIKE ?1 AND lower(_data)=lower(?1)"

.line 1485
.restart local v3 #where:Ljava/lang/String;
const/4 v0, 0x1

new-array v4, v0, [Ljava/lang/String;

const/4 v0, 0x0

aput-object p1, v4, v0
:try_end_1
.catchall {:try_start_1 .. :try_end_1} :catchall_0
.catch Landroid/os/RemoteException; {:try_start_1 .. :try_end_1} :catch_0

.restart local v4 #selectionArgs:[Ljava/lang/String;
goto :goto_1

.line 1510
.end local v3 #where:Ljava/lang/String;
.end local v4 #selectionArgs:[Ljava/lang/String;
.end local v13 #hasWildCards:Z
:catchall_0
move-exception v0

if-eqz v12, :cond_6

.line 1511
invoke-interface {v12}, Landroid/database/Cursor;->close()V

.line 1510
:cond_6
throw v0

.line 1508
:catch_0
move-exception v0

.line 1510
if-eqz v12, :cond_7

.line 1511
:goto_3
invoke-interface {v12}, Landroid/database/Cursor;->close()V

.line 1514
:cond_7
const/4 v5, 0x0

goto :goto_2

.line 1510
.restart local v3 #where:Ljava/lang/String;
.restart local v4 #selectionArgs:[Ljava/lang/String;
.restart local v13 #hasWildCards:Z
:cond_8
if-eqz v12, :cond_7

goto :goto_3
.end method

Recompile and you're good to go.

Hope this helps.