Scan Android Sd Card for New Files

Scan Android SD card for new files

Since the last answer I posted apparently wasn't an appropriate method, I found another method here. You basically create a wrapper class, initialize it, and then call the scan() method. Very helpful post. Let me know if this isn't appropriate either.

How to scan SD card for particular extension of files in android?

public void walkdir(File dir) {
String pdfPattern = ".pdf";

File[] listFile = dir.listFiles();

if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {

if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(pdfPattern)){
//Do what ever u want

}
}
}
} }

To search on the whole sdcard call this function usingwalkdir(Environment.getExternalStorageDirectory());

How to invoke Android media scanner for vendor provided external SD card, not the one obtained via getExternalStorage()

I took a look in Android open source code (Android 4.1)

There is file called /packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java

It has following code:

 @Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Uri uri = intent.getData();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
// scan internal storage
scan(context, MediaProvider.INTERNAL_VOLUME);
} else {
if (uri.getScheme().equals("file")) {
// handle intents related to external storage
String path = uri.getPath();
String externalStoragePath = Environment.getExternalStorageDirectory().getPath();

Log.d(TAG, "action: " + action + " path: " + path);
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
// scan whenever any volume is mounted
scan(context, MediaProvider.EXTERNAL_VOLUME);
} else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) &&
path != null && path.startsWith(externalStoragePath + "/")) {
scanFile(context, path);
}
}
}
}

As you can see it will check for ACTION_MEDIA_MOUNT (which you use) and it will call scan(). However, it will use hardcoded MediaProvier.EXTERNAL_VOLUME (instead of passed file URI).

Answering your question, it doesn't make sense for you to change your code. Any URI with file schema will work the same.

However, there is a chance that vendor will modify this code.

And one more thing. Android 4.2 introduced multiuser notion and each user has his/her own external storage. Based on this, shown code may have changed.

Update 1

It's interesting. Initially, I just looked through part of MediaScannerReceiver and was under impression that it will scan only one external volume. However, after you told me that you looked through the code and asked whether it will work. I investigate a little bit further and found that it will search all mountable volumes (as you said).

As I understand it goes through following execution path (it in kind of pseudo-java code to disregard all instantiations and so on)

  • MediaScannerReceiver.onReceive calls scan(context, MediaProvider.EXTERNAL_VOLUME);
  • MediaScannerReceiver.scan calls context.startService(
    new Intent(context, MediaScannerService.class).putExtras(args)); where args contain key/value pair "volume"=MediaProvider.EXTERNAL_VOLUME)
  • MediaScannerService.onStartCommand calls mServicehandler.sendMessage
  • MediaScannerService.ServiceHandler.handleMessage receives a message and calls equivalent of
    scan(StorageManager.getVolumePaths(),MediaProvider.EXTERNAL_VOLUME)
  • MediaScannerService.scan calls MediaScanner.scanDirectories
  • MediaScanner goes through each directory one by one

Taking into account that "StorageManager.getVolumePaths()" should return all mountable volumes, I think you should be fine with your current code (it will scan all volumes).



Related Topics



Leave a reply



Submit