title: Android storage access framework persistable permission not granted during asyntask
body: <p>I set the intent flags here:</p>
  
  <pre><code>public void createAlbum(View view) {
      Intent intent = new Intent();
      intent.setType("image/*");
      intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
      intent.addCategory(Intent.CATEGORY_OPENABLE);
      intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
      intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
      String intentChooserDialog = getResources().getString(R.string.pick_image_dialog);
      startActivityForResult(Intent.createChooser(intent, intentChooserDialog), PICK_IMAGE);
  }
  </code></pre>
  
  <p>Here in my activity result I set the persistable permissions as they are explained <a href="https://developer.android.com/guide/topics/providers/document-provider.html" rel="nofollow">here</a>:</p>
  
  <pre><code>@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == PICK_IMAGE) {
          // Get image selection
          if (data != null) {
              ClipData clipData = data.getClipData();
              if (clipData != null) {
                  int clipDataCount = clipData.getItemCount();
                  if (clipDataCount &gt; 1) {
                      int clipIndex = 0;
                      BitmapUri[] bitmapUris = new BitmapUri[clipDataCount];
                      while (clipIndex &lt; clipDataCount) {
                          ClipData.Item item = clipData.getItemAt(clipIndex);
                          Uri uri = item.getUri();
                          int takeFlags = data.getFlags();
                          takeFlags &amp;= Intent.FLAG_GRANT_READ_URI_PERMISSION;
                          getContentResolver().takePersistableUriPermission(uri, takeFlags);
                          BitmapUri bitmapUri = new BitmapUri(Uri.decode(uri.toString()),            data.getFlags());
                          bitmapUris[clipIndex] = bitmapUri;
                          clipIndex++;
                      }
                      DatabaseAlbumWriter dab = new DatabaseAlbumWriter(this);
                      dab.execute(bitmapUris);
                  }
              } else {
                  int oneImageSelected = 1;
                  BitmapUri[] bitmapUris = new BitmapUri[oneImageSelected];
                  Uri uri = data.getData();
                  int takeFlags = data.getFlags();
                  takeFlags &amp;= Intent.FLAG_GRANT_READ_URI_PERMISSION;
                  getContentResolver().takePersistableUriPermission(uri, takeFlags);
                  BitmapUri bitmapUri = new BitmapUri(Uri.decode(uri.toString()), data.getFlags());
                  bitmapUris[0] = bitmapUri;
                  DatabaseAlbumWriter dab = new DatabaseAlbumWriter(this);
                  dab.execute(bitmapUris[0]);
              }
          }
      }
  }
  </code></pre>
  
  <p>After the content style Uri’s are saved to a sqlite3 database, I want to load them in a gridview, but asynchronously as described <a href="https://developer.android.com/training/displaying-bitmaps/process-bitmap.html" rel="nofollow">here</a>. Here is my implementation of decodeSampledBitmapFromResource from the training guide. An object with a decoded String of the Uri and the persistable permission from the original intent are passed in. These values were saved in the database and loaded here again.</p>
  
  <pre><code>        protected Bitmap decodeSampledBitmapFromResource(BitmapUri bitmapUri, int reqWidth, int reqHeight) {
          Bitmap bitmap = null;
          Uri uri = Uri.parse(bitmapUri.getStringUri());
          int permissions = bitmapUri.getPermissions();
          permissions &amp;= Intent.FLAG_GRANT_READ_URI_PERMISSION;
          try {
              getContentResolver().takePersistableUriPermission(uri, permissions);
              ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
              FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
              // First decode with inJustDecodeBounds=true to check dimensions
              final BitmapFactory.Options options = new BitmapFactory.Options();
              options.inJustDecodeBounds = true;
              Rect rect = new Rect(-1,-1,-1,-1);
              BitmapFactory.decodeFileDescriptor(fileDescriptor, rect, options);
              // Calculate inSampleSize
              options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
              // Decode bitmap with inSampleSize set
              options.inJustDecodeBounds = false;
              bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, rect, options);
              parcelFileDescriptor.close();
          } catch (NullPointerException | SecurityException | IOException e) {
              e.printStackTrace();
          }
          return bitmap;
      }
  }
  </code></pre>
  
  <p>It is here that I get this error: </p>
  
  <pre><code>05-08 14:17:40.850 2728-2813/com.example.app W/System.err: java.lang.SecurityException: No persistable permission grants found for UID 10058 and Uri 0 @ content://com.android.providers.media.documents/document/image:32
  05-08 14:17:40.850 2728-2813/com.example.app W/System.err:     at android.os.Parcel.readException(Parcel.java:1599)
  05-08 14:17:40.850 2728-2813/com.example.app W/System.err:     at android.os.Parcel.readException(Parcel.java:1552)
  05-08 14:17:40.850 2728-2813/com.example.app W/System.err:     at android.app.ActivityManagerProxy.takePersistableUriPermission(ActivityManagerNative.java:4217)
  05-08 14:17:40.850 2728-2813/com.example.app W/System.err:     at android.content.ContentResolver.takePersistableUriPermission(ContentResolver.java:1703)
  05-08 14:17:40.850 2728-2813/com.example.app W/System.err:     at com.example.app.AlbumCreator$BitmapWorkerTask.decodeSampledBitmapFromResource(AlbumCreator.java:395)
  05-08 14:17:40.850 2728-2813/com.example.app W/System.err:     at com.example.app.AlbumCreator$BitmapWorkerTask.doInBackground(AlbumCreator.java:343)
  05-08 14:17:40.850 2728-2813/com.example.app W/System.err:     at com.example.app.AlbumCreator$BitmapWorkerTask.doInBackground(AlbumCreator.java:329)
  </code></pre>
  
  <p>If I load the bitmap from a parcelFileDescriptor with BitmapFactory.decodeFileDescriptor in the activity result I do not get this error. So, from what I can tell, the AsyncTask is a different user with different privileges. Anyone know how to assign persistable permissions to an AsyncTask? I’d rather not load bitmaps on the UI thread. </p>
  
  <p>Here is my manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.app"&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
      &lt;uses-permission android:name="android.permission.MANAGE_DOCUMENTS" /&gt;
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme"&gt;
          &lt;activity
              android:name=".AlbumCreator"
              android:label="@string/app_name"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;!--
               ATTENTION: This was auto-generated to add Google Play services to your project for
               App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
          --&gt;
          &lt;meta-data
              android:name="com.google.android.gms.version"
              android:value="@integer/google_play_services_version" /&gt;
          &lt;activity
              android:name=".AlbumEditor"
              android:label="@string/title_activity_album_editor"
              android:parentActivityName=".AlbumCreator"&gt;
              &lt;meta-data
                  android:name="android.support.PARENT_ACTIVITY"
                  android:value="com.example.app.AlbumCreator" /&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>