Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

sample - 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.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...