Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaScanner ServiceConnectionLeaked

I use this class to scan my app image in SDCard.

public class SingleMediaScanner implements MediaScannerConnectionClient {

private MediaScannerConnection mMs;
private File mFile;

public SingleMediaScanner(Context context, File f) {
    mFile = f;
    mMs = new MediaScannerConnection(context, this);
    mMs.connect();
}

@Override
public void onMediaScannerConnected() {
    mMs.scanFile(mFile.getAbsolutePath(), null);
}

@Override
public void onScanCompleted(String path, Uri uri) {
    mMs.disconnect();
}
}

It worked but in my LogCat it always shown an error:

12-29 16:44:16.022: ERROR/ActivityThread(21807): Activity com.cny.ecard.CustomListDialog has leaked ServiceConnection android.media.MediaScannerConnection@450fb8e0 that was originally bound here
12-29 16:44:16.022: ERROR/ActivityThread(21807): android.app.ServiceConnectionLeaked: Activity com.cny.ecard.CustomListDialog has leaked ServiceConnection android.media.MediaScannerConnection@450fb8e0 that was originally bound here
12-29 16:44:16.022: ERROR/ActivityThread(21807):     at android.app.ActivityThread$PackageInfo$ServiceDispatcher.<init>(ActivityThread.java:1121)
12-29 16:44:16.022: ERROR/ActivityThread(21807):     at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
12-29 16:44:16.022: ERROR/ActivityThread(21807):     at android.app.ContextImpl.bindService(ContextImpl.java:863)
12-29 16:44:16.022: ERROR/ActivityThread(21807):     at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
12-29 16:44:16.022: ERROR/ActivityThread(21807):     at android.media.MediaScannerConnection.connect(MediaScannerConnection.java:117)
12-29 16:44:16.022: ERROR/ActivityThread(21807):     at com.cny.ecard.SingleMediaScanner.<init>(SingleMediaScanner.java:19)

What is the problem? Thanks.

like image 481
user430926 Avatar asked Dec 29 '11 08:12

user430926


2 Answers

For scanning a single file, or scanning a file from a context in which you can't bind to a service (e.g., from a BroadcastReceiver), user430926's comment deserves to be a separate answer:

Uri contentUri = Uri.fromFile(file);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
like image 57
acj Avatar answered Nov 20 '22 18:11

acj


the android media scanner service like bind service, so when you finish the scan, you can use context.unbindService to unbind.

like image 2
idiottiger Avatar answered Nov 20 '22 17:11

idiottiger