title: Android issues reading images from persistent Uri
body: <p>My app lets the user select images and then it animates the images. It's a simple thing and I want it to run on 4.2 and onwards. It works perfectly now.</p>
  
  <p>The app is supposed to remember the chosen images and let these be default the next time the user runs the app - when the app is restarted. </p>
  
  <p>This works beautifully on my Galaxy Nexus (4.2.1) but not so well on my Galaxy S8+ (7.0). Since I love how it works on 4.2.1, it's a bit frustrating that it can't simply run the same on later platforms. </p>
  
  <p>This is inside <code>onCreate()</code>:</p>
  
  <pre><code>((ImageButton) findViewById(R.id.imagex))
      .setOnClickListener(new View.OnClickListener() {
          public void onClick(View arg0) {
              Intent intent = new Intent();
              intent.setType("image/*");
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(Intent.createChooser(intent,
                      "Select image"), SELECT_IMAGEX);
          }
      });
  </code></pre>
  
  <p>From <code>onActivityResult</code>:</p>
  
  <pre><code>public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  
      // [snip unimportant code] 
      Uri uri = intent.getData();
      Bitmap bitmap = getBitmap(uri);
  
      final int takeFlags = intent.getFlags()
                      &amp; (Intent.FLAG_GRANT_READ_URI_PERMISSION
                      | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      try {
          getContentResolver().takePersistableUriPermission(uri, takeFlags);
      } catch (SecurityException e) {
          Log.w(myLog, "SecurityException: " + e.getMessage());
      }
      // [snip]
      editor.putString("imagex", uri.toString());
      editor.commit();
  }
  </code></pre>
  
  <p>Later, the app is restarted and this part of the method <code>onCreate()</code> goes:</p>
  
  <pre><code>String tmp = preferences.getString("imagex", null);
  if (tmp != null) {
      uri = Uri.parse(tmp);
      Bitmap bitmap  = getBitmap(uri);
      // etc
  }
  </code></pre>
  
  <p>On the S8+, getBitmap() gives an exception <code>Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS</code> (meaning the method returns null).</p>
  
  <p>It's quite confusing why it would complain about one single missing permission TWICE in one single exception message. By the way, it still does so after adding <code>&lt;uses-permission android:name="android.permission.MANAGE_DOCUMENTS" /&gt;</code>.</p>
  
  <p>Reading on this great site, I find people talking about</p>
  
  <pre><code>final int takeFlags = intent.getFlags()
             &amp; (Intent.FLAG_GRANT_READ_URI_PERMISSION
             | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  getContentResolver().takePersistableUriPermission(selectedImageUri, takeFlags);
  </code></pre>
  
  <p>which is some fairly modern invention, but unfortunately it just causes this on the S8+</p>
  
  <pre><code>SecurityException: No persistable permission grants found
  </code></pre>
  
  <p>and on the Galaxy Nexus it simply crashes by </p>
  
  <pre><code>java.lang.NoSuchMethodError
  </code></pre>