title: Retrieve file path from caught DownloadManager intent
body: <p>Since Android 4.2 if a user downloads some file in the browser the DownloadManager is used. If the user clicks the 'download complete' notification an Intent is and was always launched. Before Android 4.2 the intent used to have the downloaded file's path in the content, such that:</p>
  
  <pre><code>intent.getData()
  </code></pre>
  
  <p>would return a String such as <code>file:///storage/emulated/0/Download/some_file.ext</code>. However, since Android 4.2 the download manager broadcasts and intent with a <code>content</code> scheme, such as <code>content://downloads/all_downloads/2334</code>.</p>
  
  <p>How do I retrieve the local file path for a downloaded file?</p>
  
  <p>I've tried the following:</p>
  
  <pre><code>public static String getRealPathFromURI(Uri contentUri, Activity activity) {
      DownloadManager downloadManager = (DownloadManager) activity.getSystemService(Activity.DOWNLOAD_SERVICE);
      String[] contentParts = contentUri.getEncodedPath().split("/");
      Cursor q = downloadManager.query(new DownloadManager.Query().setFilterById(Integer.parseInt(contentParts[contentParts.length - 1])));
      if (q == null) {
          // Download no longer exists
          return null;
      }
      q.moveToFirst();
      return q.getString(q.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
  }
  </code></pre>
  
  <p>But the cursor never returns any rows (so <code>q.getCount() == 0</code> and therefor the last <code>return</code> statement throws an exception). Also, the hack by parsing the download file id from the Uri seems odd.</p>
  
  <p><strong>UPDATE:</strong> I have also tried:</p>
  
  <pre><code>input = getActivity().getContentResolver().openInputStream(contentUri);
  </code></pre>
  
  <p><s>but this returns an error stating</s></p>
  
  <p><s>Permission Denial: reading com.android.providers.downloads.DownloadProvider uri content://downloads/all_downloads/2334 from pid=30950, uid=10064 requires android.permission.ACCESS_ALL_DOWNLOADS, or grantUriPermission()</s></p>
  
  <p><s>Clearly I can't access the downloads (as my app did not initiate them - the browser did) through the <code>ContentProvider</code>.</s></p>