java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED on KitKat only
Asked Answered
W

3

16

I am using the DownloadManager to download images off our server and I am placing the files in the externalFilesDir.

I am send out a broadcast intent because I don't want these downloaded images to appear in the gallery.

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

I only tested this on my Galaxy S3 Jelly Bean 4.3 prior and it was working, but when I tested it on KitKat 4.4 it crashes the app.

Is there a better way to not have the files downloaded from the DownloadManager not appear in the phone gallery?

Stack Trace

06-05 17:34:41.940: E/AndroidRuntime(15410): FATAL EXCEPTION: main
06-05 17:34:41.940: E/AndroidRuntime(15410): Process: com.walintukai.lfdate, PID: 15410
06-05 17:34:41.940: E/AndroidRuntime(15410): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.walintukai.lfdate (has extras) } in com.walintukai.lfdate.SocketIOService$1@42359f40
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Handler.handleCallback(Handler.java:733)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Looper.loop(Looper.java:136)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.ActivityThread.main(ActivityThread.java:5057)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at java.lang.reflect.Method.invoke(Method.java:515)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at dalvik.system.NativeStart.main(Native Method)
06-05 17:34:41.940: E/AndroidRuntime(15410): Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=15410, uid=10135
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Parcel.readException(Parcel.java:1465)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Parcel.readException(Parcel.java:1419)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2390)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at com.walintukai.lfdate.SocketIOService$1.onReceive(SocketIOService.java:111)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
06-05 17:34:41.940: E/AndroidRuntime(15410):    ... 9 more
Wayfaring answered 6/6, 2014 at 0:43 Comment(1)
Your question is ambiguous: do you want to prevent the files from appearing in the Gallery, or do you want to ensure they appear there? To prevent files in a certain directory from appearing in Gallery (or other media libraries), place a file named .nomedia in the same directory. To get files to appear immediately, run a media scan as described in the answers below.Alongside
H
48

It seems that google is trying to prevent this from KITKAT.
Looking at core/rest/AndroidManifest.xml you will notice that broadcast android.intent.action.MEDIA_MOUNTED is protected now. Which means it is a broadcast that only the system can send.

<protected-broadcast android:name="android.intent.action.MEDIA_MOUNTED" />

The following should work for all versions:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    final Uri contentUri = Uri.fromFile(outputFile); 
    scanIntent.setData(contentUri);
    sendBroadcast(scanIntent);
} else {
    final Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
    sendBroadcast(intent);
}

If the above is not working try the following:

According to this post you need another way to fix it.
Like using MediaScannerConnection or ACTION_MEDIA_SCANNER_SCAN_FILE.

MediaScannerConnection.scanFile(this, new String[] {

file.getAbsolutePath()},

null, new MediaScannerConnection.OnScanCompletedListener() {

public void onScanCompleted(String path, Uri uri)

{


}

});
Hokanson answered 6/6, 2014 at 0:59 Comment(4)
@SiddharthVyas According to the documentation MediaScannerConnection API is supposed to work from API level 1, so all versions of android should work. developer.android.com/reference/android/media/…Hokanson
its not working and not refreshing images and folder in gallery using android version below 4.4.4Marcheshvan
The second solution is working. Thank you for response.Washerman
What to do if I do not have access to application source code?Juvenility
L
28

I know it's late but try this, it works in every version:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(out); // out is your output file
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
} else {
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
        Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
Lordan answered 11/2, 2015 at 7:33 Comment(2)
how to get file in protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);Korwun
Thanks, this helped with my question: #37996741Semiweekly
S
0

As a variant

public static void refreshDir(@NonNull Context context, @NonNull File dir) {

    notifyFileDeleted(context, dir);

    File[] files = dir.listFiles();

    if (files != null) {
        for (File file : files) {
            if (file.isDirectory())
                refreshDir(context, file);
            else
                notifyFileCreated(context, file);
        }
    }
}

public static void notifyFileCreated(@NonNull Context context, @NonNull File file) {
        //disable notification for non-existent files, folders and files from a location inaccessible via mtp
        if (file.exists() && file.isFile() && file.getAbsolutePath().indexOf("/data/data/") != 0) {
            MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        }
    }

public static void notifyFileDeleted(@NonNull Context context, @NonNull File file) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            context.getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file)));
        }
        else{
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            context.sendBroadcast(intent);
        }
    }
Sylvanite answered 3/12, 2021 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.