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

6

15

I got this class:

import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.util.Log;

public class MediaScannerWrapper implements  
MediaScannerConnection.MediaScannerConnectionClient {
    private MediaScannerConnection mConnection;
    private String mPath;
    private String mMimeType;


    // filePath - where to scan; 
    // mime type of media to scan i.e. "image/jpeg". 
    // use "*/*" for any media
    public MediaScannerWrapper(Context ctx, String filePath, String mime){
        mPath = "/sdcard/DCIM/Camera";
        mMimeType = "jpg";
        mConnection = new MediaScannerConnection(ctx, this);
    }

    // do the scanning
    public void scan() {
        mConnection.connect();
    }

    // start the scan when scanner is ready
    public void onMediaScannerConnected() {
        mConnection.scanFile(mPath, mMimeType);
        Log.w("MediaScannerWrapper", "media file scanned: " + mPath);
    }

    public void onScanCompleted(String path, Uri uri) {
        // when scan is completes, update media file tags
    }
}

How to use it in the other class? I don't know how to properly use classes, I tried but nothing is working. I do something wrong, but I don't know what, can someone help me with this.

Montane answered 23/2, 2012 at 14:28 Comment(7)
Are you talking about that post?Leges
Oh, didn't found that post, thanks!Montane
Could you still help me, I am really bad with classes and such things in Java.Montane
What is it that you want to do exactly?Vladamar
With my program, I change the name of a picture, then when I try to open the image again (same app, without closing it) it doesn't reconize the picture anymore. However, after a mediascan, it does reconize it. So I want to scan a path or a file, so the app reconize the name change.Montane
you will need to update the content database, see my answer belowVladamar
I won't go with a full Java and classes course, because there's already a lot of resources on the web for that. I don't mean to be rude; I just feel you'll get better documentation by reading full-fledged tutorials and how-to's. That said, in your case, just make sure you instantiate your helper class in your code path, then call the scan method on the instance. Something like that: MediaScannerWrapper myScanner = new MediaScannerWrapper(); myScanner.scan();Leges
M
10

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.

Montane answered 27/2, 2012 at 9:0 Comment(3)
It will trigger the mediascanner but it will scan the all the files (except those folder with .nomedia file) in the SD Card. It will take time to scan depending on the no of files in your sd card.Potoroo
Intent.ACTION_MEDIA_MOUNTED will break on Android 4.4+ devices as the intent is now limited to system applications.Antigua
Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTEDHolography
F
52

The Story

Before Android 4.4, we could just send a broadcast to trigger the media scanner on any particular file, or folder or even on the root of the storage. But from 4.4 KitKat, this have been fixed by the Android Developers.

Why do I say fixed? The reason is simple. Sending a broadcast using MEDIA_MOUNTED on the root directory is very expensive. Running the Media Scanner is an expensive operation and the situation gets even worse when the user has got a lot of files in the storage and deep folder structures.

Before Android 4.4

Keep it straight and simple. If you are targeting your app before Android 4.4. But keep in mind not to use it on the root directory unless absolutely necessary.

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

From Android 4.4

There are two ways for you.

i) The first one is very similar to the previous example, but may not work efficiently and is not recommended too.

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

ii) Now, let us move on to the most recommended and efficient solution to this problem.

Add the file paths of the files which have been updated, like this, in a String type ArrayList

ArrayList<String> toBeScanned = new ArrayList<String>();
toBeScanned.add(item.getFilePath());

Now you need to run scanFile() static method of the MediaScannerConnection class and pass the String array containing the list of all the files which have been updated and needs to be media scanned.

You can also put a listener to respond when the scanning has been finished for individual files.

String[] toBeScannedStr = new String[toBeScanned.size()];
                toBeScannedStr = toBeScanned.toArray(toBeScannedStr);

                MediaScannerConnection.scanFile(getActivity(), toBeScannedStr, null, new OnScanCompletedListener() {

                    @Override
                    public void onScanCompleted(String path, Uri uri) {
                        System.out.println("SCAN COMPLETED: " + path);

                    }
                });
Froze answered 1/8, 2014 at 18:33 Comment(16)
way ii converts directories into files, see #31158382Fateful
way i has no effect (both comments refer to Android 5)Fateful
When I copy a file to the Ringtones folder, and let Media Scanner scan it, it shows up right away on the sound picker ! However, after I have my app delete the file from the Ringtones folder, I also let media scanner scan it (onScanCompleted() triggers with uri = null), yet, media picker still shows that file. What to do after deleting the file so that media picker no longer shows it ?Sericin
Can't we just put the path of the file directly to Intent.ACTION_MEDIA_SCANNER_SCAN_FILE or Intent.ACTION_MEDIA_MOUNTED? So it will be like sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(filePath)));Biak
this should be the more appropriate answer instead of the accepted answer below, as the accepted answer does not scan on specific path, +1Newton
@Aritra Roy I am creating a gallery app. And In that app whenever a user renames a folder, I want mediascanner to scan and remove earlier album and add new album. So any best way to do this. And can I give Uri of a folder to sendBroadcast.Anthurium
@AnkeshkumarJaisansaria Have you followed this method?Froze
Yes but not able to refresh media storeAnthurium
@AnkeshkumarJaisansaria I have just tried the exact same thing for 4.4, works like a charm.Froze
@Aritra Roy Passing file uri is working but I want to pass Folder Uri as the whole folder name has changedAnthurium
Please help for Triggering media scan on folderAnthurium
You can just loop through the files inside the folder and pass each of them to be scanned. Have tried it now, and its working for me.Froze
MediaScannerConnection.scanFile used to work previously, but for unknown reason, that's doesn't work anymore on my device, so i had to change to Intent.ACTION_MEDIA_SCANNER_SCAN_FILEBiak
MediaScannerConnection.ScanFile works for me, but only if I also add the parent directory in the String array. See also https://mcmap.net/q/144026/-nexus-4-not-showing-files-via-mtpBremen
but how to use it for empty directories? Use the "not recommended" method?Roentgen
ii) - didn't work on Android 7, Nexus 5x: sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uriToMyDir)); The folder was in the root of internal memory. Only approach with scanFile helped.Wiggler
M
10

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.

Montane answered 27/2, 2012 at 9:0 Comment(3)
It will trigger the mediascanner but it will scan the all the files (except those folder with .nomedia file) in the SD Card. It will take time to scan depending on the no of files in your sd card.Potoroo
Intent.ACTION_MEDIA_MOUNTED will break on Android 4.4+ devices as the intent is now limited to system applications.Antigua
Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTEDHolography
V
7

In Android, there is a content database which is used by the media scanner to keep track of all the media content present on the device.

When Android boots up, the mediascanner service is launched and runs through the entire external storage to find if there is any new media content if it finds one then,

  • It adds an entry of that media content into the content database
  • Each entry in the content database contains metadata of the media content like Name, date, file size, type of file, etc..
  • So when you make a modification to a media content, you will need to update the content database also.
  • If the content database is not update then other applications also will not be able to access that particular media content.
  • Running the media scanner just updates the content database

Instead of running the media scanner, you can update the content database yourself and it should resolve the problem.

Here is an explanation on how to insert, delete, update using the content resolver. (Search for the section "Inserting, Updating, and Deleting Data")

Edit: There is a sample code in this answer. Check for the answer by Janusz.

Vladamar answered 24/2, 2012 at 11:35 Comment(2)
Thanks, I will go on that when I am finished with something else.Montane
He, added my own answer, just found this code, even more easier to use :)Montane
T
6
   File file = new File(absolutePath);
   Uri uri = Uri.fromFile(file);
   Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
   sendBroadcast(intent);
Tosh answered 19/3, 2014 at 6:34 Comment(1)
I will break in android 7.0Franciscofranciska
A
1
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}

Reference: http://developer.android.com/training/camera/photobasics.html#TaskGallery

The Add the Photo to a Gallery Section

Azores answered 27/3, 2016 at 14:13 Comment(0)
B
0

As @Aritra Roy's answer, i decide to make an experiment about this issue. What i got here are:

  • Intent.ACTION_MEDIA_MOUNTED and Intent.ACTION_MEDIA_SCANNER_SCAN_FILE can accept individual file path, so sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(filePath))); or sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(filePath))); will be valid.
  • If you use individual file path with Intent.ACTION_MEDIA_MOUNTED on Kitkat or above, your application will still crash
  • If you use Intent.ACTION_MEDIA_SCANNER_SCAN_FILE or MediaScannerConnection on device lower than Kitkat, your application will not force close, but the method will just simply not working as you want.

From that experiment, i think the best method to handle is

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
            //something that you want to do
        }
    });
} else {
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + imagePath)));
}

Let me know if i missed something

Biak answered 15/6, 2016 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.