Skip to content Skip to sidebar Skip to footer

Initialize Mediascanner At Beginning Of Android Program

I have an Android program that starts off by loading some of the user's media. Occasionally the program crashes -- both on an emulator and on an actual phone. I've found this is be

Solution 1:

You can add a BroadcastReceiver in your code and handle ACTION_MEDIA_SCANNER_FINISHED broadcast. It is actually being sent by MediaScannerService once it's done scanning. Hope the sample code below helps.

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
filter.addDataScheme("file");
registerReceiver(mReceiver, filter);   

BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
                Toast.makeText(MediaScannerActivity.this, "Scan complete.", Toast.LENGTH_SHORT).show();
            }
        }
    };

Post a Comment for "Initialize Mediascanner At Beginning Of Android Program"