DownloadManager.Request.setNotificationVisibility fails with SecurityException: invalid value for visibility: 2
Asked Answered
D

2

64

I'm trying to use DownloadManager in a Service class of mine:

    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    //imageUri is a valid Uri
    Request downloadRequest= new Request(imageUri); 
    //without this line, it works
    downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
    //subpath is valid
    downloadRequest.setDestinationInExternalFilesDir(this, null, subPath);
    downloadManager.enqueue(downloadRequest);

Without setNotificationVisibility, this code works well. But it displays a notification, that I don't want, because I use this code for lazy-loading images in a ListView.

I understand, this notification can be hidden with

downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);

But in that case, my application crashes with:

02-19 02:24:24.055: E/AndroidRuntime(2572): java.lang.SecurityException: Invalid value for visibility: 2 02-19 02:24:24.055: E/AndroidRuntime(2572): at android.os.Parcel.readException(Parcel.java:1327) 02-19 02:24:24.055: E/AndroidRuntime(2572): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181) 02-19 02:24:24.055: E/AndroidRuntime(2572): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) 02-19 02:24:24.055: E/AndroidRuntime(2572): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:415) 02-19 02:24:24.055: E/AndroidRuntime(2572): at android.content.ContentResolver.insert(ContentResolver.java:730) 02-19 02:24:24.055: E/AndroidRuntime(2572): at android.app.DownloadManager.enqueue(DownloadManager.java:885) 02-19 02:24:24.055: E/AndroidRuntime(2572): at MyService.getImage(MyService.java:112)

Whats' wrong with setNotificationVisibility? How can I had the notification?

Dioptrics answered 19/2, 2012 at 1:55 Comment(2)
downloadRequest.setNotificationVisibility(...) helped me...thanksPenza
before using this method :downloadRequest.setNotificationVisibility(...) please bear in mind that it is Added in API level 11Adai
E
164

You need the following permission in the manifest as per documentation:

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

Note
Most of the manifest permissions will be autocompleted by Android Studio, but this will not be auto-completed.

Eucken answered 21/2, 2012 at 9:12 Comment(4)
as indicated in the documentation... Thanks!Dioptrics
downloadRequest.setVisibleInDownloadsUi(....) another useful API to hide it from Download UI.Penza
I don't see this permissionDimpledimwit
This issue still appears to be happening on 5.1.1 (Nexus 5) and the M preview.Mirza
S
0

but also need to add that permission to rn-fetch-blob's manifest that is not given in it.

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
Sound answered 11/5, 2020 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.