29

I'm using the MediaScannerConnection example code from the API Demos

The snippet I'm using is:

MediaScannerConnection.scanFile(
    context,
    new String[] { permFile.getAbsolutePath() }, 
    null,
    new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {

            android.util.Log.i("ExternalStorage", "Scanned " + path + ":");
            android.util.Log.i("ExternalStorage", "-> uri=" + uri);
        }
});

When I run this code, I get an FC dialog with the following from the LogCat:

4-20 23:17:45.988: ERROR/ActivityThread(3015): Activity com.my.package.name has leaked ServiceConnection android.media.MediaScannerConnection@40715c70 that was originally bound here
04-20 23:17:45.988: ERROR/ActivityThread(3015): android.app.ServiceConnectionLeaked: Activity com.my.package.name has leaked ServiceConnection android.media.MediaScannerConnection@40715c70 that was originally bound here

What am I doing wrong?

FYI, I'm running this from a background thread using AsyncTask.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Shellabarger
  • 1,523
  • 2
  • 15
  • 23
  • 1
    I have the feeling that MediaScannerConnection is leaking the listener. Somehow it doesn't get cleaned up and there isn't a method to reset the listener. I am facing the same kind of issue at this moment. Did you solve this in the mean time? – eMich Apr 17 '12 at 16:25
  • I don't remember. I posted this a year ago. I'll have to go back and check. – David Shellabarger Apr 24 '12 at 01:55
  • I'm still seeing this on KitKat. Pretty sure this is an Android issue, not a usage issue. – Justin Sep 07 '14 at 14:53

3 Answers3

29

I noticed the same kind of error message using the code snippet provided with the documentation of Environment.getExternalStoragePublicDirectory.

The code works fine as expected and makes a new file visible in the device gallery, but at the same time prints the error about the leaked ServiceConnection.

Looking at the internal Android code of the MediaScannerConnection it seems some kind of mechanism exists to stop the service after the last file. Maybe it doesn't work when given only one file?

I ended up using an entirely different solution by informing the MediaScanner via Intent. This works fine too and it is not producing any warnings:

Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(permFile); // With 'permFile' being the File object
mediaScannerIntent.setData(fileContentUri);
this.sendBroadcast(mediaScannerIntent); // With 'this' being the context, e.g. the activity

It seems this is the preferred way, and it is mentioned in the Android Training about Taking Photos too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sunadorer
  • 3,855
  • 34
  • 42
12

Use getApplicationContext() instead.

Pepijn
  • 1,439
  • 17
  • 16
0

I had this problem with the voice recognizer when I would change layouts, etc.

All I had to do was add a unregisterReceiver, kind of like this in the onActivityResult:

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    unregisterReceiver(mReceiver);
    super.onDestroy();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Droidster
  • 127
  • 1
  • 2
  • 8