• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps App crashing when trying to show system notification after downloading a new file

have and android application which downloads pdf documents from a backend service using the following code, executed in a class that implements android.os.AsyncTask that runs in background:

Java:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
   String path = Environment.DIRECTORY_DOWNLOADS;
   ContentValues contentValues = new ContentValues();
   contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, file.getName());
   contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,path);
   ContentResolver resolver = context.getContentResolver();
   Uri uri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
   try {
       OutputStream os = resolver.openOutputStream(uri);
       byte data[] = new byte[4096];
       int count;
       while ((count = is.read(data)) != -1) {
           os.write(data, 0, count);
       }
       os.flush();
       IOUtils.closeQuietly(os);
       IOUtils.closeQuietly(is);
   } catch (FileNotFoundException e) {
       Log.e("DOWNLOAD","File not found",e);
   } catch (IOException e) {
       Log.e("DOWNLOAD","Error downloading file",e);
   }
} else {
   OutputStream output = new FileOutputStream(file);
   byte data[] = new byte[4096];
   int count;
   while ((count = is.read(data)) != -1) {
       output.write(data, 0, count);
   }
   IOUtils.closeQuietly(output);
   IOUtils.closeQuietly(is);
}

After the download of the file, that completes withou errors, I try to show a notification in the system bar with the following code inside the onPostExecute() method of the AsyncTask:

Java:
Uri fileUri = FileProvider.getUriForFile(context,
       context.getResources().getString(R.string.file_provider_authority),
       file);

Intent viewFileIntent = new Intent(Intent.ACTION_VIEW);
viewFileIntent.setDataAndType(fileUri, mimeType);
viewFileIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.addCompletedDownload(fileName, fileName, true, mimeType, file.toString(), file.length(), true);

if ( context.getPackageManager().queryIntentActivities(
       viewFileIntent,
       PackageManager.MATCH_DEFAULT_ONLY).size() > 0 ) {
   context.startActivity(viewFileIntent);
} else {
   DSSErrorUtils.showI18n(context, context.getString(R.string.MESSAGE_NO_APP_FOR_FILE));
}

When I run the emulator with Android 10 - SDK Version 30, the app crashes with the following error (generated in method manager.addCompletedDownload() ):

java.lang.SecurityException: No permission to write to /storage/emulated/0/Download/document_704.pdf: Neither user 10148 nor current process has android.permission.WRITE_EXTERNAL_STORAGE

The same code executed on emulator or devices with Android 11 - SDK Version 30 runs without problems. Also with Android versions < 10 it works correctly.

Any help would be very appreciated! Thanks!
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones