Invoking the MediaScanner in just a single directory (android)
Asked Answered
C

3

6

I have a problem trying to get MediaScanner to scan just one directory.

My app takes pictures, and saves them to sd-card/DCIM/AppPictures/, and obviously I need to invoke the MediaScanner for them to show up in the gallery app. The following code DOES work for my purpose:

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

But it just seems so wasteful! I don't want to use resources trying to scan the whole SDcard when I know exactly where the files are. I have tried the following too:

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

But this fails to yield any results. Can someone PLEASE let me know the proper way to get this done?

Calcariferous answered 30/11, 2012 at 0:43 Comment(0)
M
1
public static void ScanMyFile(String strFilePath) {
    // Tell the media scanner so it is available to the user.
    MediaScannerConnection.scanFile(null, new String[] { strFilePath }, null, 
            new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {

        }
    });
}
Maxim answered 7/11, 2013 at 16:7 Comment(0)
L
0

In my app, I'm also downloading pictures.

When my main activity (that download pictures) is started, I initiate a mediascanner connection. The downloading is sequential, once a picture has arrived, I store the file name in a string (named currentFile below) and "connect" the media scanner :

public class SomeActivity 
          extends Activity implements MediaScannerConnectionClient {

    ....

    protected MediaScannerConnection mMs;

    ....

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        ....
        mMs = new MediaScannerConnection(this, this);
        ....
    }

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

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


}

To launch the scanning I just do :

currentFile = "/mnt/sdcard/someLocationToScan/somefile.jpg";
mMs.connect();
Landmark answered 29/3, 2013 at 16:58 Comment(0)
Q
0

I used your same answer slightly modified to scan just Music directory.

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

So I tried the following for your case and it worked, hopefully scanning all DCIM isn't too much.

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

Quinine answered 10/4, 2022 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.