title: Why does my application crash when it tries to open a file after the phone restarts?
body: <p>I'm developing an application that recieves a file from the user and inserts the URI into a SQLite db:</p>
  
  <pre><code>    public void getBook(){                     // called when the user wants to give a file to the app
          Intent chooseFile = new Intent(Intent.ACTION_OPEN_DOCUMENT);
          chooseFile.setType("text/plain");
          chooseFile = Intent.createChooser(chooseFile, "Choose a file");
          startActivityForResult(chooseFile, FILE_CHOOSER_REQUEST_CODE);
      }
  
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == FILE_CHOOSER_REQUEST_CODE &amp;&amp; resultCode == Activity.RESULT_OK) {
              Uri u = data.getData();
              addBook(u);   // addBook adds u.toString() to the db
          }
      }
  </code></pre>
  
  <p>My application uses the following code to read the file (it gets <code>u</code> by using <code>Uri.parse()</code> on the string from the db):</p>
  
  <pre><code>    private void readFile(Uri u, String lang, String name){
          try {
              InputStream is = getContentResolver().openInputStream(u);  //the code crashes on this line
              \\ some code...
          }
          catch(java.io.IOException e){
              Toast.makeText(MainActivity.this, R.string.file_not_found, Toast.LENGTH_SHORT).show();
          }
      }
  </code></pre>
  
  <p>This works fine even when I reopen the app, but after the phone gets restarted I'm getting the following error:</p>
  
  <pre><code>2020-03-06 11:31:49.687 5664-5664/com.example.android.readme E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.android.readme, PID: 5664
      java.lang.SecurityException: Permission Denial: opening provider com.android.providers.downloads.DownloadStorageProvider from ProcessRecord{93bfa52 5664:com.example.android.readme/u0a163} (pid=5664, uid=10163) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
          at android.os.Parcel.readException(Parcel.java:2013)
          at android.os.Parcel.readException(Parcel.java:1959)
          at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4758)
          at android.app.ActivityThread.acquireProvider(ActivityThread.java:5836)
          at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2526)
          at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1780)
          at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1394)
          at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247)
          at android.content.ContentResolver.openInputStream(ContentResolver.java:967)
          at com.example.android.readme.MainActivity.readFile(MainActivity.java:162)
          at com.example.android.readme.MainActivity.access$300(MainActivity.java:25)
          at com.example.android.readme.MainActivity$1.onItemClick(MainActivity.java:84)
          at android.widget.AdapterView.performItemClick(AdapterView.java:318)
          at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
          at android.widget.AbsListView$PerformClick.run(AbsListView.java:3127)
          at android.widget.AbsListView$3.run(AbsListView.java:4042)
          at android.os.Handler.handleCallback(Handler.java:790)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6494)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  </code></pre>
  
  <p>Why does it happen? (I do use <code>ACTION_OPEN_DOCUMENT</code> in my code.)</p>
  
  <p>Thanks in advance for any help!</p>