How to trigger MediaScan on Nexus 7?

前端 未结 3 1426
感情败类
感情败类 2020-12-03 12:45

Because I want to make sure the MediaStore has the latest information without having to reboot I\'d like to trigger the MediaScanner using the popular way I found on SO

3条回答
  •  情歌与酒
    2020-12-03 13:18

    My answer is a little late, but it might help those, who save a new file, and would like to extend the media store by just that file on Android Kitkat: On Android Kitkat the intent ACTION_MEDIA_MOUNTED is blocked for non-system apps (I think, because scanning the whole filesystem is pretty expensive). But it is still possible to use the intent ACTION_MEDIA_SCANNER_SCAN_FILE to add a file to the media store:

    File f = new File(path to the file you would like to add to the media store ...);
    try {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(f);
        mediaScanIntent.setData(uri);
        sendBroadcast(mediaScanIntent);
    } catch(Exception e) {
        ... 
    }
    

提交回复
热议问题