Wpis

Media scanning

Media scanning

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

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

But now, instead of scan proccess, one can get

1
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:

1
2
3
4
5
6
7
8
9
10
11
12
    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);
                }
            });
    }
Ten post jest udostępniony na licencji CC BY 4.0 przez autora.