title: How to get image real path by using uri without using MediaStore.Images.Media.DATA
body: <p>I found that <code>MediaStore.Images.Media.DATA</code> is deprecated, so I want to use another way to get image real path.</p>
  
  <p>Here is my new way to get uri and title of image, but I don't know how to get real path.</p>
  
  <p>I print uri, title, path:</p>
  
  <p>photoUri: content://media/external/images/media/1605</p>
  
  <p>title: IMG_20191223143418</p>
  
  <p>photoUri.getPath: /external/images/media/1605</p>
  
  <p>But I want just like this: /storage/emulated/0/DCIM/1733/IMG_20191223143418.jpg</p>
  
  <pre><code>public static String getRealPathFromUri(Context context, Uri uri) throws IOException {
  
      String filePath = null;
  
      if (DocumentsContract.isDocumentUri(context, uri)) {
  
                  String documentId = DocumentsContract.getDocumentId(uri);
                  if (isMediaDocument(uri)) { // MediaProvider.
                      String id = documentId.split(":")[1];
  
                      String selection = MediaStore.Images.Media._ID + "=?";
                      String[] selectionArgs = {id};
  
                      filePath = getDataColumnImage(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);
                  } else if (isDownloadsDocument(uri)) { // DownloadsProvider.
                      Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));
                      filePath = getDataColumnImage(context, contentUri, null, null);
                  }
          }
          else if ("content".equalsIgnoreCase(uri.getScheme())) {
              filePath = getDataColumnImage(context, uri, null, null);
          }
          else if ("file".equals(uri.getScheme())) {
              filePath = uri.getPath();
          }else
              return filePath = null;
          return filePath;
  }
  
  
  private static boolean isMediaDocument(Uri uri) {
      return "com.android.providers.media.documents".equals(uri.getAuthority());
  }
  
  private static boolean isDownloadsDocument(Uri uri) {
      return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  }
  
  private static String getDataColumnImage(Context context, Uri uri, String selection, String[] selectionArgs) {
  
      String path = null;
  
      String[] projection = new String[]{
                  MediaStore.Files.FileColumns._ID,
                  MediaStore.Images.Media.DATE_TAKEN,
                  MediaStore.Images.Media.WIDTH,
                  MediaStore.Images.Media.HEIGHT,
                  MediaStore.MediaColumns.TITLE,
                  MediaStore.Images.Media.MIME_TYPE,
          };
          Cursor cursor = null;
          try {
              cursor = context.getContentResolver().query(
                      uri,
                      projection,
                      selection,
                      selectionArgs,
                      null
              );
              if (cursor != null &amp;&amp; cursor.moveToFirst()) {
                  int idColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID);
                  int titleColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
  
                  Uri photoUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getString(idColumn));
                  String title = cursor.getString(titleColumn);
  
                  Log.e("TAG", "photoUri: " + photoUri);
                  Log.e("TAG", "title: " + title);
                  Log.e("TAG", "photoUri.getPath: " + photoUri.getPath());
              }
          } catch (Exception e) {
              if (cursor != null) {
                  cursor.close();
              }
          }
          return path;
  
          // MediaStore.Images.Media.DATA is deprecated, so I want to use the other way as mentioned above to get real path.
          /*String[] projection = new String[]{MediaStore.Images.Media.DATA};
              Cursor cursor = null;
              try {
                  cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
                  if (cursor != null &amp;&amp; cursor.moveToFirst()) {
                      int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
                      path = cursor.getString(columnIndex);
                  }
              } catch (Exception e) {
                  if (cursor != null) {
                      cursor.close();
                  }
              }
              return path;*/
  }
  </code></pre>