How to trigger MediaScan on Nexus 7?

前端 未结 3 1427
感情败类
感情败类 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:21

    Here's the sample code based on CommonsWare's answer:

    MediaScannerConnection.scanFile(activity, new String[]{path}, null,
                                    new MediaScannerConnection.OnScanCompletedListener() {
        @Override
        public void onScanCompleted(final String path, final Uri uri) {
            Log.i(TAG, String.format("Scanned path %s -> URI = %s", path, uri.toString()));
        }
    });
    

    Even though in most of the cases, where one knows the files to be added/updated/etc. to the MediaStore, one should follow CommonsWare's answer, I wanted to post the my solution where I need to do it the rough way because I don't know the file paths. I use this mostly for testing/demoing:

    Uri uri = Uri.fromFile(Environment.getExternalStorageDirectory());
    activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, uri));
    

    BTW, no permissions are necessary for either solution.

提交回复
热议问题