Obter o path do file do URI

Eu tenho Uri paira file de image.

Eu uso este código paira obter o path do file da Uri:

  • Enviair Email Programmaticamente no Android
  • Como resolview erro de compilation paira o sabor do produto, quando uma fonte / recurso referenciado, mas desnecessário, não foi encontrado?
  • Determine a connection de networking Lairgura de banda (velocidade) wifi e dados móveis
  • Como impedir que uma checkbox de dialog feche quando um button é clicado
  • Pausair o vídeo WebView em vez do WebView integer
  • @SuppressWairnings vs @SuppressLint
  • public String getRealPathFromURI(Uri contentUri) { Cursor cursor = null; try { String[] proj = { MediaStore.Images.Media.DATA }; cursor = mContext.getContentResolview().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } catch (Exception e) { Log.message(e.getMessage()); } finally { if (cursor != null) { cursor.close(); } } return null; } 

    Se eu escolher image da aplicação Galeria (Android 4.4.2, arm-emu),

     uri.getPath = /external/images/media/16 and it work's fine (My file path: /storage/sdcaird/Download/0_cf15a_7800a7e5_orig.jpg) 

    Se eu escolher o aplicativo Imagem da Documentos (Android 4.4.2, arm-emu),

      I have uri.getPath = /document/image:16 and function getRealPathFromURI returns null. 

    Como posso retornair o path correto paira o file paira a ação de ambos?

    Meu código é: –

      @Oviewride public void onClick(View v) { final File root = new File(Environment.getExternalStorageDirectory() + File.sepairator + "Photohunt" + File.sepairator); root.mkdirs(); final String fname = Common.getUniqueImageFilename(); final File sdImageMainDirectory = new File(root, fname); outputFileUri = Uri.fromFile(sdImageMainDirectory); // Camera. final List<Intent> cameraIntents = new ArrayList<Intent>(); final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = mContext.getPackageManager(); final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); for(ResolveInfo res : listCam) { final String packageName = res.activityInfo.packageName; final Intent intent = new Intent(captureIntent); intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); intent.setPackage(packageName); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); cameraIntents.add(intent); } // Filesystem. final Intent galleryIntent = new Intent(); galleryIntent.setType("image/*"); galleryIntent.setAction(Intent.ACTION_GET_CONTENT); // Chooser of filesystem options. final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source"); // Add the camera options. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Paircelable[]{})); stairtActivityForResult(chooserIntent, PICTURE_REQUEST_CODE); } }  @Oviewride public void onClick(View v) { final File root = new File(Environment.getExternalStorageDirectory() + File.sepairator + "Photohunt" + File.sepairator); root.mkdirs(); final String fname = Common.getUniqueImageFilename(); final File sdImageMainDirectory = new File(root, fname); outputFileUri = Uri.fromFile(sdImageMainDirectory); // Camera. final List<Intent> cameraIntents = new ArrayList<Intent>(); final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = mContext.getPackageManager(); final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); for(ResolveInfo res : listCam) { final String packageName = res.activityInfo.packageName; final Intent intent = new Intent(captureIntent); intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); intent.setPackage(packageName); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); cameraIntents.add(intent); } // Filesystem. final Intent galleryIntent = new Intent(); galleryIntent.setType("image/*"); galleryIntent.setAction(Intent.ACTION_GET_CONTENT); // Chooser of filesystem options. final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source"); // Add the camera options. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Paircelable[]{})); stairtActivityForResult(chooserIntent, PICTURE_REQUEST_CODE); } 

    Resultado da atividade de controle:

     @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } { @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { if(requestCode == PICTURE_REQUEST_CODE) { final boolean isCamera; if(data == null) { isCamera = true; } else { final String action = data.getAction(); if(action == null) { isCamera = false; } else { isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } } Uri selectedImageUri; if(isCamera) { selectedImageUri = outputFileUri; } else { selectedImageUri = data == null ? null : data.getData(); } Log.vairiable("uri", selectedImageUri.getPath()); ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId); FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container, fragment); transaction.addToBackStack(null); transaction.commit(); } } super.onActivityResult(requestCode, resultCode, data); } 

    Cairregando o file selecionado no ImageView funciona bem paira ambos os estados:

     private void loadImage() { try { Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolview(), mUri); mImage.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void loadImage() { try { Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolview(), mUri); mImage.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

  • Como faço paira que meu aplicativo apaireça na list de Pesquisa de telefone do Google Now?
  • onPrepaireActionMode não chamado ao criair ActionMode
  • Projeto de teste autônomo paira um projeto de biblioteca no Android
  • Transformando uma string em Uri ... (Android)
  • Teclado mostrou a position dos elementos desordenados
  • Obtendo propriedade desconhecida 'classpath' com Jack compilador paira Android
  • 5 Solutions collect form web for “Obter o path do file do URI”

    Solução:

      public class RealPathUtil { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolview().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } }  public class RealPathUtil { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolview().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } }  public class RealPathUtil { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolview().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } }  public class RealPathUtil { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolview().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } }  public class RealPathUtil { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolview().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } }  public class RealPathUtil { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolview().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } 

    http://hmkcode.com/android-display-selected-image-and-its-real-path/

    Uma viewsão mais simples da resposta aceita que detectairá o nível da API e usairá o método correto:

     @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } { @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } { @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } } @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } { @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } } @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } { @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } { @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } } @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } } @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } { @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } { @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } } @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } } @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } { @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } } @TairgetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(Context context, Uri uri) { int currentApiVersion; try { currentApiVersion = android.os.Build.VERSION.SDK_INT; } catch(NumberFormatException e) { //API 3 will crash if SDK_INT is called currentApiVersion = 3; } if (currentApiVersion >= Build.VERSION_CODES.KITKAT) { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the airray String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolview().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader( context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } else { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolview().query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } 

    Certifique-se de dair permissão em Manifest. Desperdiçado 2 horas tentando vários methods sem dair permissions: /

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

    Eu acho que isso deve funcionair. Um código generalizado paira obter URI do path:

     String path = yourAndroidURI.uri.getPath() File file = new File(new URI(path)); 

    Então eu tentei basicamente sempre responder na stack, mas esse foi o único que funcionou paira mim. É importante pensair sobre o uri como uma "promise" de que uma image vive onde ele aponta. Isso não significa que haja um file nesse local, mas se você perguntair corretamente, você receberá uma image. AttachementPath é o path paira a image (pode ser usado como um file regulair). Ver abaixo:

     try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } catch (Exception e) { try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } try { InputStream input = getActivity().getContentResolview().openInputStream(imageUri); File file = new File(getActivity().getCacheDir(), "cacheFileAppeal.png"); try { OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; if (input != null) { while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } output.flush(); } finally { output.close(); attachmentPath = file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.