Scan Android SD card for new files

后端 未结 6 1749
感情败类
感情败类 2020-11-27 19:59

My app allows a user to save an image to their SD card. But I\'m not sure how to make it appear in the gallery until you unmount and remount the SD card. I have googled for

6条回答
  •  难免孤独
    2020-11-27 20:23

    Here is another way to force scan:

    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,"uri to file"));
    

    And then system will fire ACTION_MEDIA_SCANNER_FINISHED broadcast so you can react on it with BroadcastReceiver

    In order to be able to receive ACTION_MEDIA_SCANNER_FINISHED broadcast, intent filter should contain data scheme:

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED);
    intentFilter.addDataScheme("file");
    context.registerReceiver(mMediaScannerFinishReceiver, intentFilter);
    

    from the doc:

    public static final String ACTION_MEDIA_SCANNER_SCAN_FILE

    Added in API level 1 Broadcast Action: Request the media scanner to scan a file and add it to the media database. The path to the file is contained in the Intent.mData field.

    and

    public static final String ACTION_MEDIA_SCANNER_FINISHED

    Added in API level 1 Broadcast Action: The media scanner has finished scanning a directory. The path to the scanned directory is contained in the Intent.mData field.

提交回复
热议问题