Skip to content Skip to sidebar Skip to footer

Mediascannerconnection Produces Android.app.serviceconnectionleaked

I'm using the MediaScannerConnection example code from the API Demos The snippet I'm using is: MediaScannerConnection.scanFile( context, new String[] { permFile.getAbsolute

Solution 1:

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:

IntentmediaScannerIntent=newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
UrifileContentUri= 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.

Solution 2:

Use getApplicationContext() instead.

Solution 3:

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 stubunregisterReceiver(mReceiver);
    super.onDestroy();
}

Post a Comment for "Mediascannerconnection Produces Android.app.serviceconnectionleaked"