Media scanning

| Author: Michał Ćwikliński

Camera Application

So far, after recording on downloading new media file in order to make system see this file developer had to fire broadcast:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + context.getExternalFilesDir(null))));

But now, instead of scan proccess, one can get

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED

The simplest solution to get rig of this exception is to use MediaScanner:

    public static void scanFile(Context context, String path) {
        MediaScannerConnection.scanFile(
            context.getApplicationContext(),
            new String[]{ path },
            null,
            new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
                    Log.e(TAG, "file " + path + " was scanned successfully: " + uri);
                }
            });
    }