content: package org.amahi.anywhere.util;
  
  import android.content.ContentUris;
  import android.content.Context;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Build;
  import android.os.Environment;
  import android.provider.DocumentsContract;
  import android.provider.MediaStore;
  
  public class PathUtil {
  
      public static String getPath(Context context, Uri uri) {
          String selection = null;
          String[] selectionArgs = null;
  
          if (DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
              if (isExternalStorageDocument(uri)) {
                  final String docId = DocumentsContract.getDocumentId(uri);
                  final String[] split = docId.split(":");
                  return Environment.getExternalStorageDirectory() + "/" + split[1];
              } else if (isDownloadsDocument(uri)) {
                  final String id = DocumentsContract.getDocumentId(uri);
                  uri = ContentUris.withAppendedId(
                      Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
              } else if (isMediaDocument(uri)) {
                  final String docId = DocumentsContract.getDocumentId(uri);
                  final String[] split = docId.split(":");
                  final String type = split[0];
                  if ("image".equals(type)) {
                      uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                  } else if ("video".equals(type)) {
                      uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                  } else if ("audio".equals(type)) {
                      uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                  }
                  selection = "_id=?";
                  selectionArgs = new String[]{split[1]};
              }
          }
          if ("content".equalsIgnoreCase(uri.getScheme())) {
              String[] projection = {MediaStore.Images.Media.DATA};
              Cursor cursor;
              cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
              if (cursor != null) {
                  int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                  if (cursor.moveToFirst()) {
                      return cursor.getString(column_index);
                  }
                  cursor.close();
              }
          } else if ("file".equalsIgnoreCase(uri.getScheme())) {
              return uri.getPath();
          }
          return null;
      }
  
      private static boolean isExternalStorageDocument(Uri uri) {
          return "com.android.externalstorage.documents".equals(uri.getAuthority());
      }
  
      private static boolean isDownloadsDocument(Uri uri) {
          return "com.android.providers.downloads.documents".equals(uri.getAuthority());
      }
  
      private static boolean isMediaDocument(Uri uri) {
          return "com.android.providers.media.documents".equals(uri.getAuthority());
      }
  }
  