Trigger Mediascanner on Specific Path (Folder), How To

Trigger mediascanner on specific path (folder), how to?

Hey I found out how to do it with a very simple code.

Just call this line of code:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

This should trigger mediascanner.

MediaScannerConnection#scanFile converts directories into files when accessing them with USB-MTP on Android 5 Lollipop

I ended up creating a dummy text file in each directory I wanted to make visible, and use scanFile on the file.

1) create directory, but don't "scan" directories

2) copy file to directory

3) run scanFile on the filePath

MediaScannerConnection.scanFile (_application, new String[] { filePath }, null, null);

Trigger mediascanner on specific path (folder), how to?

Hey I found out how to do it with a very simple code.

Just call this line of code:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

This should trigger mediascanner.

Android How to use MediaScannerConnection scanFile

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));

How to use MediaScannerConnection scanFile?

    MediaScannerConnection.scanFile(this, new String[]{file.getPath()},
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// now visible in gallery
}
});

Scan Android SD card for new files

Since the last answer I posted apparently wasn't an appropriate method, I found another method here. You basically create a wrapper class, initialize it, and then call the scan() method. Very helpful post. Let me know if this isn't appropriate either.

How to add taken photo to MediaStore

On most devices, all you need to do is wait a little while and the new photos will be detected automatically.

If you want to preform an immediate refresh to the gallery, you need to use the MediaScanner class, It will refresh the gallery - remove deleted photos, add new ones and so on...

public void refreshGallery() {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
String newPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image
File file = new File(newPhotoPath);
Uri contentUri = Uri.fromFile(file);
scanIntent.setData(contentUri);
sendBroadcast(scanIntent);
}

Hope this helped!

How to invoke Android media scanner for vendor provided external SD card, not the one obtained via getExternalStorage()

I took a look in Android open source code (Android 4.1)

There is file called /packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java

It has following code:

 @Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Uri uri = intent.getData();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
// scan internal storage
scan(context, MediaProvider.INTERNAL_VOLUME);
} else {
if (uri.getScheme().equals("file")) {
// handle intents related to external storage
String path = uri.getPath();
String externalStoragePath = Environment.getExternalStorageDirectory().getPath();

Log.d(TAG, "action: " + action + " path: " + path);
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
// scan whenever any volume is mounted
scan(context, MediaProvider.EXTERNAL_VOLUME);
} else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) &&
path != null && path.startsWith(externalStoragePath + "/")) {
scanFile(context, path);
}
}
}
}

As you can see it will check for ACTION_MEDIA_MOUNT (which you use) and it will call scan(). However, it will use hardcoded MediaProvier.EXTERNAL_VOLUME (instead of passed file URI).

Answering your question, it doesn't make sense for you to change your code. Any URI with file schema will work the same.

However, there is a chance that vendor will modify this code.

And one more thing. Android 4.2 introduced multiuser notion and each user has his/her own external storage. Based on this, shown code may have changed.

Update 1

It's interesting. Initially, I just looked through part of MediaScannerReceiver and was under impression that it will scan only one external volume. However, after you told me that you looked through the code and asked whether it will work. I investigate a little bit further and found that it will search all mountable volumes (as you said).

As I understand it goes through following execution path (it in kind of pseudo-java code to disregard all instantiations and so on)

  • MediaScannerReceiver.onReceive calls scan(context, MediaProvider.EXTERNAL_VOLUME);
  • MediaScannerReceiver.scan calls context.startService(
    new Intent(context, MediaScannerService.class).putExtras(args)); where args contain key/value pair "volume"=MediaProvider.EXTERNAL_VOLUME)
  • MediaScannerService.onStartCommand calls mServicehandler.sendMessage
  • MediaScannerService.ServiceHandler.handleMessage receives a message and calls equivalent of
    scan(StorageManager.getVolumePaths(),MediaProvider.EXTERNAL_VOLUME)
  • MediaScannerService.scan calls MediaScanner.scanDirectories
  • MediaScanner goes through each directory one by one

Taking into account that "StorageManager.getVolumePaths()" should return all mountable volumes, I think you should be fine with your current code (it will scan all volumes).



Related Topics



Leave a reply



Submit