My app allows user to save files on an external SD card besides the getExternalStorage() path. I understand that Android does not have notion of external SD cardas such but as we know many device manufacturers provide an extra SD card slot for the tablets/phones. And the path to this particular sdcard could be vendor dependent.
My app provides the user a preference where he/she can provide that vendor path to the SD card that’s different than the path returned by getExternalStorage().
Previously I would use following code to invoke media scanner,
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
But now I am wondering whether the following code might work:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ "/someotherpath/blah/");
Would it work? I do not have such a device with an extra SD card slot to test it out, your opinion would be useful for me.
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:
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)
new Intent(context, MediaScannerService.class).putExtras(args)); where args contain key/value pair “volume”=MediaProvider.EXTERNAL_VOLUME)
scan(StorageManager.getVolumePaths(),MediaProvider.EXTERNAL_VOLUME)
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).