title: Android: Files: Unable to get file path from content URI when file is selected from RECENT section in file browser
body: <p>I was trying to fetch file path from Uri given by data.data!! onActivityResult() method in android. When I selected the DCIM folder in File browser I was able to get the file path but when I selected the same file from RECENT section in file browser I was not getting the file path.</p>
  
  <p>Code on Button click:</p>
  
  <pre><code>                                        val intent = Intent();
                                          intent.setType("image/*");
                                          val mimeTypes: Array&lt;String&gt; = arrayOf("image/*", "application/pdf")
                                          intent.putExtra(Intent.EXTRA_MIME_TYPES,mimeTypes)
                                          intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                                          intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                          startActivityForResult(Intent.createChooser(intent, "Select File"), 101);
  </code></pre>
  
  <p>Code on returning data from onActivityResult() method:</p>
  
  <pre><code>var selectedImagePathFB:String = ""
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      if (resultCode == RESULT_OK) {
          if (requestCode == 101) {
              val selectedImageUri:Uri = data?.data!!
              selectedImagePathFB = getPath(this@MyContactUsActivity,selectedImageUri);
              //selectedImagePathFB = AppUtils().getRealPathFromURI(this@MyContactUsActivity,selectedImageUri)
              val url: String = selectedImagePathFB
  
          }
      }
  }
  </code></pre>
  
  <p>and getPath(Context,Uri) is common found code from stackoverflow by paulburke:</p>
  
  <pre><code> public static String getPath(final Context context, final Uri uri) {
  
  
      // DocumentProvider
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT &amp;&amp; DocumentsContract.isDocumentUri(context, uri)) {
  
          if (isExternalStorageDocument(uri)) {
              final String docId = DocumentsContract.getDocumentId(uri);
              final String[] split = docId.split(":");
              final String type = split[0];
  
              return Environment.getExternalStorageDirectory() + "/" + split[1];
  
              // TODO handle non-primary volumes
          }
          // DownloadsProvider
          else if (isDownloadsDocument(uri)) {
              try {
                  final String id = DocumentsContract.getDocumentId(uri);
                  //Log.d(TAG, "getPath: id= " + id);
                  final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
                  return getDataColumn(context, contentUri, null, null);
              }catch (Exception e){
                  e.printStackTrace();
                  List&lt;String&gt; segments = uri.getPathSegments();
                  if(segments.size() &gt; 1) {
                      String rawPath = segments.get(1);
                      if(!rawPath.startsWith("/")){
                          return rawPath.substring(rawPath.indexOf("/"));
                      }else {
                          return rawPath;
                      }
                  }
              }
          }
          // MediaProvider
          else if (isMediaDocument(uri)) {
              final String docId = DocumentsContract.getDocumentId(uri);
              final String[] split = docId.split(":");
              final String type = split[0];
  
              Uri contentUri = null;
              if ("image".equals(type)) {
                  contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
              } else if ("video".equals(type)) {
                  contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
              } else if ("audio".equals(type)) {
                  contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
              }
  
              final String selection = "_id=?";
              final String[] selectionArgs = new String[]{
                      split[1]
              };
  
              return getDataColumn(context, contentUri, selection, selectionArgs);
          }
      }
      // MediaStore (and general)
      else if ("content".equalsIgnoreCase(uri.getScheme())) {
  
          // Return the remote address
          if (isGooglePhotosUri(uri))
              return uri.getLastPathSegment();
  
          return getDataColumn(context, uri, null, null);
      }
      // File
      else if ("file".equalsIgnoreCase(uri.getScheme())) {
          return uri.getPath();
      }
  
      return null;
  }
  public static String getDataColumn(Context context, Uri uri, String selection,
                                     String[] selectionArgs) {
  
      Cursor cursor = null;
      final String column = "_data";
      final String[] projection = {
              column
      };
  
      try {
          cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                  null);
          if (cursor != null &amp;&amp; cursor.moveToFirst()) {
  
  
              final int column_index = cursor.getColumnIndexOrThrow(column);
              return cursor.getString(column_index);
          }
      }catch (Exception e){
          e.printStackTrace();
      }finally {
          if (cursor != null)
              cursor.close();
      }
      return "nodata";
  }
  
  public static boolean isExternalStorageDocument(Uri uri) {
      return "com.android.externalstorage.documents".equals(uri.getAuthority());
  }
  
  public static boolean isDownloadsDocument(Uri uri) {
      return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  }
  
  public static boolean isMediaDocument(Uri uri) {
      return "com.android.providers.media.documents".equals(uri.getAuthority());
  }
  
  public static boolean isGooglePhotosUri(Uri uri) {
      return "com.google.android.apps.photos.content".equals(uri.getAuthority());
  }
  </code></pre>
  
  <p>I am testing on android Q device. Why is that when selecting file from RECENT section in file browser i am not getting any file path meanwhile selecting same file in STORAGE section is returning file path? And is there a workaround also for converting files like PDF or Images in Base64 string if not from file path? </p>