1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Gabriele Cestra, Nov 10, 2021.

  1. Gabriele Cestra

    Thread Starter

    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:

    Code (Java):
    1. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
    2.    String path = Environment.DIRECTORY_DOWNLOADS;
    3.    ContentValues contentValues = new ContentValues();
    4.    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, file.getName());
    5.    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,path);
    6.    ContentResolver resolver = context.getContentResolver();
    7.    Uri uri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
    8.    try {
    9.        OutputStream os = resolver.openOutputStream(uri);
    10.        byte data[] = new byte[4096];
    11.        int count;
    12.        while ((count = is.read(data)) != -1) {
    13.            os.write(data, 0, count);
    14.        }
    15.        os.flush();
    16.        IOUtils.closeQuietly(os);
    17.        IOUtils.closeQuietly(is);
    18.    } catch (FileNotFoundException e) {
    19.        Log.e("DOWNLOAD","File not found",e);
    20.    } catch (IOException e) {
    21.        Log.e("DOWNLOAD","Error downloading file",e);
    22.    }
    23. } else {
    24.    OutputStream output = new FileOutputStream(file);
    25.    byte data[] = new byte[4096];
    26.    int count;
    27.    while ((count = is.read(data)) != -1) {
    28.        output.write(data, 0, count);
    29.    }
    30.    IOUtils.closeQuietly(output);
    31.    IOUtils.closeQuietly(is);
    32. }
    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:

    Code (Java):
    1. Uri fileUri = FileProvider.getUriForFile(context,
    2.        context.getResources().getString(R.string.file_provider_authority),
    3.        file);
    4.  
    5. Intent viewFileIntent = new Intent(Intent.ACTION_VIEW);
    6. viewFileIntent.setDataAndType(fileUri, mimeType);
    7. viewFileIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    8.  
    9. DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    10. manager.addCompletedDownload(fileName, fileName, true, mimeType, file.toString(), file.length(), true);
    11.  
    12. if ( context.getPackageManager().queryIntentActivities(
    13.        viewFileIntent,
    14.        PackageManager.MATCH_DEFAULT_ONLY).size() > 0 ) {
    15.    context.startActivity(viewFileIntent);
    16. } else {
    17.    DSSErrorUtils.showI18n(context, context.getString(R.string.MESSAGE_NO_APP_FOR_FILE));
    18. }
    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!
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...