0

I'm writing a simple image viewer for Android. My app have the "Open file from Gallery" button. So, when I've choosed the image from Gallery, it must return the path to file into my app, but it return a strange Uri, which looks like content://media/storage/2ch/30128.

How I can get an absolute file path from this Uri? Here's some code for launching Gallery:


 Intent i = new Intent(); i.setType("image/*"); i.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(i, "Выберите файл"), PICTURE_REQUEST_CODE); 

P.S. Excuse for my English

1
  • check out stackoverflow.com/questions/17546101/… but it's a little screwed up since android 4.4. because they added the new document picker and that delivers different URIs... Commented Dec 27, 2013 at 12:04

3 Answers 3

2

Use this code

protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){ Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Then select this as answer, by clicking on green select button it will help to others
Sure, I will select it as right answer. I just forgot to do this :)
how to make the same with pdf file ?
0

You can also get image using URI :

try this

try { Uri selectedImage = selectedImageUri; //getContentResolver().notifyChange(selectedImage, null); ContentResolver cr = getContentResolver(); Bitmap bitmap; bitmap = android.provider.MediaStore.Images.Media .getBitmap(cr, selectedImage); rptImage.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) .show(); Log.e("Camera", e.toString()); } 

OR simply this :

Try with:

ImageView.setImageUri(Uri.fromFile(new File("/sdcard/cats.jpg"))); 

Or with:

ImageView.setImageUri(Uri.parse(new File("/sdcard/cats.jpg").toString())); 

2 Comments

Thanks for quick reply, but I need to get the file's path for further operations, not only for opening.
try this : File f = new File(uri.getPath());
0

try below code

protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_FROM_FILE && resultCode == Activity.RESULT_OK) { Uri mImageCaptureUri = data.getData(); String [] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery( contentUri, proj, null, null,null); if (cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); } } } 

here variable path will return path to your selected image.

Comments