3

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ahmed
  • 14,503
  • 22
  • 92
  • 150

2 Answers2

1

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).

Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
  • Thanks Victor, so is it safe to assume the call scan(context, MediaProvider.EXTERNAL_VOLUME); will scan all sdcards ? . I drilled down the android code and it seems to fetch the scan directories from com.android.internal.R.xml.storage_list in the MountService class – Ahmed Jan 31 '13 at 17:59
  • 2Ahmed: I investigate a little bit more. Look at Update1 in my answer – Victor Ronin Jan 31 '13 at 23:42
  • Thanks Victor, after code drill I came to similar conclusion, hope it works in production :) thank you. – Ahmed Feb 01 '13 at 12:52
  • Why wasn't there any upvote for this detailed questions? Nice answer, thank you – schlenger May 03 '18 at 05:48
0

For Api 8 and above you can use this

 MediaScannerConnection.scanFile(this,
      new String[] { file.toString() }, null,
      new MediaScannerConnection.OnScanCompletedListener() {
  public void onScanCompleted(String path, Uri uri) {
      Log.i("ExternalStorage", "Scanned " + path + ":");
      Log.i("ExternalStorage", "-> uri=" + uri);
  }
 });

This scans only an individual file and you can give any path here, including alternative external storage.

Arveen
  • 868
  • 6
  • 18
  • Thanks but this code does not suit my case I have to scan the whole sdcard path I do not have individual files recorded – Ahmed Jan 31 '13 at 17:54
  • You can also pass directory in the file path. This will trigger a scan for that folder only. – Arveen Jan 31 '13 at 18:35
  • This means it will also work for something like "/mnt/sdcard2" – Arveen Jan 31 '13 at 18:43