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.

7
  • 1
    Are you talking about that post?
    – XMoby
    Feb 23, 2012 at 16:34
  • Oh, didn't found that post, thanks!
    – Bigflow
    Feb 24, 2012 at 7:11
  • Could you still help me, I am really bad with classes and such things in Java.
    – Bigflow
    Feb 24, 2012 at 7:42
  • What is it that you want to do exactly?
    – bluefalcon
    Feb 24, 2012 at 11:04
  • 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.
    – Bigflow
    Feb 24, 2012 at 11:14

6 Answers 6

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);

                    }
                });
16
  • 1
    way ii converts directories into files, see stackoverflow.com/questions/31157882/…
    – OneWorld
    Jul 1, 2015 at 9:48
  • 2
    way i has no effect (both comments refer to Android 5)
    – OneWorld
    Jul 1, 2015 at 10:02
  • 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 ? Dec 21, 2015 at 15:38
  • 1
    this should be the more appropriate answer instead of the accepted answer below, as the accepted answer does not scan on specific path, +1
    – am05mhz
    Jun 25, 2016 at 2:24
  • 1
    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. Mar 3, 2017 at 10:05
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.

3
  • 5
    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.
    – artsylar
    Apr 16, 2012 at 5:28
  • 4
    Intent.ACTION_MEDIA_MOUNTED will break on Android 4.4+ devices as the intent is now limited to system applications.
    – Nj Subedi
    Jan 17, 2015 at 9:44
  • Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED May 6, 2020 at 14:34
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.

2
  • Thanks, I will go on that when I am finished with something else.
    – Bigflow
    Feb 24, 2012 at 14:02
  • He, added my own answer, just found this code, even more easier to use :)
    – Bigflow
    Feb 27, 2012 at 8:57
6
   File file = new File(absolutePath);
   Uri uri = Uri.fromFile(file);
   Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
   sendBroadcast(intent);
1
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.