Is it possible to scan folders recursively using MediaScanner

1.3k Views Asked by At

I have a basic question on MediaScanner pls help me in understanding this better.

MediaScanner for a folder returns only info about folder that is scanned but not contents of a folder for any media files present in it. If a folder is scanned for ex. /storage/extSdCard/DCIM/Camera which has few .jpg files in it, I get scan response about the folder and not the about files in it. logs below,

D/MediaScannerConnection(29105): Connected to MediaScanner
D/MediaScannerConnection(29105): Scanning file 7klwibgf7fxlKdCbid7DCRV7Cbvfib

D/MyLog(29105): onScanCompleted(path,uri), path: /storage/extSdCard/DCIM/Camera, Uri: content://media/external/file/4334

My question, Is it possible to get events back when ever a media file is added to folder of my interest in this case it is, /storage/extSdCard/DCIM/Camera

    public void MyScanner(String filename){
        File fileCheck = new File(filename);
        conn = new MediaScannerConnection(this,this);
        conn.connect();
}

@Override
    public void onMediaScannerConnected() {
        Log.d(TAG,"onMediaScannerConnected() to MediaScanner Service");
        conn.scanFile(filename, null);
    }

@Override
    public void onScanCompleted(String path, Uri uri) {
        if(path!=null && uri!=null)
        Log.d(TAG,"onScanCompleted(), path: "+path+", Uri: "+uri.toString());
        Log.d(TAG,"Converting file uri to FilePath: "+getRealPathFromURI(uri)); 
        }
1

There are 1 best solutions below

1
On
  First One Code....

 scanPhoto(imageFileName.toString());



public void scanPhoto(final String imageFileName)
{
  msConn = new MediaScannerConnection(PreviewDemo1.this,new MediaScannerConnectionClient()
  {
                public void onMediaScannerConnected()
                {
                    msConn.scanFile(imageFileName, null);
                    Log.i("msClient obj  in Photo Utility","connection established");
                }
                public void onScanCompleted(String path, Uri uri)
                {
                    msConn.disconnect();
                    Log.i("msClient obj in Photo Utility","scan completed");
                }
    });
    msConn.connect();
 } 

Second one code..

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