dzwillpower
9/6/2013 - 6:54 AM

gistfile1.java

package com.example.app;

import java.io.File;

import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.util.Log;

public class MyMediaScannerConnectionClient implements MediaScannerConnectionClient {

    private File mScanDir;

    private MediaScannerConnection mScanner;

    public MyMediaScannerConnectionClient(File dir) {
        mScanDir = dir;
    }

    @Override
    public void onMediaScannerConnected() {
        File[] files = mScanDir.listFiles();
        for (File f : files) {
            String path = f.getAbsolutePath()
            mScanner.scanFile(path, null);
            Log.i("MyMediaScannerConnectionClient", "Media Scan completed on file: path=" + path);

        }
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        Log.i("MyMediaScannerConnectionClient", "Media Scan completed on directory: path=" + path + " uri=" + uri);
        mScanner.disconnect();
        mScanner = null;
    }

    public void setScanner(MediaScannerConnection scanner) {
        mScanner = scanner;
    }

}