7

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.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
user430926
  • 4,017
  • 13
  • 53
  • 77
  • possible duplicate of [MediaScannerConnection produces android.app.ServiceConnectionLeaked](http://stackoverflow.com/questions/5739140/mediascannerconnection-produces-android-app-serviceconnectionleaked) – K-ballo Dec 20 '12 at 05:22
  • @user430926 i m getting this error when using correct answer 12-29 11:08:10.331: E/AndroidRuntime(29810): java.lang.RuntimeException: Unable to resume activity {com.mygallery/com.mygallery.ImageShownActivity}: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=29810, uid=10385 – Erum Dec 29 '14 at 06:10

2 Answers2

15

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);
acj
  • 4,821
  • 4
  • 34
  • 49
  • 5
    One-liner: `sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));` – XåpplI'-I0llwlg'I - Dec 27 '12 at 14:50
  • @acj i m getting this error when using this code 12-29 11:08:10.331: E/AndroidRuntime(29810): java.lang.RuntimeException: Unable to resume activity {com.mygallery/com.mygallery.ImageShownActivity}: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=29810, uid=10385 – Erum Dec 29 '14 at 06:09
  • 1
    @ErumHannan The permissions have been tightened in recent versions of Android. You may need to use `MediaScannerConnection.scanFile()` instead. – acj Dec 29 '14 at 16:24
  • 2
    This method is no more applicable because starting from Nougat, file cannot be exposed in a public intent and you receive a security exception – greywolf82 Aug 15 '16 at 15:55
  • @greywolf82 is there any documentation for that breaking change in Nougat? – Undefined function Apr 27 '21 at 18:09
2

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

idiottiger
  • 5,147
  • 2
  • 25
  • 21
  • public void onScanCompleted(String path, Uri uri) { mMs.disconnect(); c.unbindService(mMs); } I have add c.unbindService(mMs); onScanCompleted but it still have that error. Any other suggestion? – user430926 Dec 29 '11 at 09:19
  • SingleMediaScanner this class has be created twice or more? – idiottiger Dec 29 '11 at 09:28
  • Yes, if i have to scan more than 1 file. I have try to use other code. onCreate: scanner = new MediaScannerConnection(CustomListDialog.this, null); Every time i scan image: scanner.connect(); scanner.scanFile(file.getAbsolutePath(), "image/png"); scanner.disconnect(); But it still give me that error msg. Am I doing something wrong? – user430926 Dec 29 '11 at 09:36
  • [LoadedApk](http://www.google.com/codesearch#cZwlSNS7aEw/frameworks/base/core/java/android/app/LoadedApk.java&ct=rc&cd=2&exact_package=android&q=leaked%20ServiceConnection&l=520) in the android source code, it some like the CustomListDialog will be destroyed, check the bind service, found the service has not be unbind. so, suggestion override `finish` method to `scanner.disconnect` to unbind service. – idiottiger Dec 29 '11 at 09:56
  • 6
    I found another solution: Uri contentUri = Uri.fromFile(file); Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE"); mediaScanIntent.setData(contentUri); sendBroadcast(mediaScanIntent); – user430926 Dec 30 '11 at 09:09