3

I'm trying to get real path from Uri(Of selected image from gallery) but This function returns always null value :

//Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {

    Cursor cursor = null;
    try {

        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = getActivity().getContentResolver().query(contentUri,  proj, null, null, null);
        cursor.moveToFirst();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        return cursor.getString(column_index);
    } finally {

        if (cursor != null) {

            cursor.close();
        }
    }
}
Luciano Rodríguez
  • 2,239
  • 3
  • 19
  • 32
Chlebta
  • 3,090
  • 15
  • 50
  • 99

1 Answers1

4

That's because the image you're selecting is not physically available on the device.

Images from gallery or other choose can come from different sources that does not provide a physical file, like a cloud storage for example.

Check this code here on how to open the Uri as a InputStream that can be used to create a copy of the file (or just read directly).

Android: Getting a file URI from a content URI?

edit:

I'm doing some extra research on it, apparently it also varies on how you request this Uri.

if you request it like this (which is the current preferred method as per Android guides):

 i = new Intent(Intent.ACTION_GET_CONTENT);
 i.addCategory(Intent.CATEGORY_OPENABLE);
 i.setType("image/*");
 startActivityForResult(i, REQUEST_STORAGE);

and it opens the KitKat style unified selector, if you choose an image from there, it will always return null. If you open the left-drawer menu, select the gallery (or a file browser), and pick an image from there, then, if it is a local file image, you will have the correct path.

on the other hand, if you request it like this (which is the old method):

 i = new Intent(Intent.ACTION_PICK);
 i.setType("image/*");
 startActivityForResult(i, REQUEST_STORAGE);

it will directly open the Gallery, and again, if it is a local file image, you will have the correct path.

So yeah, I still don't believe there's a way to force local only, but at least you have more information to make an informed decision on your coding.

Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144
  • So is there any way to show only the images available on the device ? – Chlebta Nov 25 '14 at 09:47
  • I don't believe I've ever seen this option. I believe that if you want to impose this limitation the only way is to create your own "photo choose". But please, if you find a way of doing it, share with us. – Budius Nov 25 '14 at 09:51
  • Now I'm gona use your solution because I'm don't have much time but in future I'm will try to make this and share it. Also can I get your mail ? – Chlebta Nov 25 '14 at 10:01
  • 1
    hi. I edit my answer with some extra information might be useful. – Budius Nov 25 '14 at 10:08
  • Thank's a Lot it really was very helpful :) – Chlebta Nov 25 '14 at 10:15
  • Interesting hack-a-round, but then it will also allow multiple file selection. If that's fine for your application, great. – Budius Nov 25 '14 at 12:02
  • Sorry, I meant intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); to allow only local file – Chlebta Nov 25 '14 at 12:09
  • It seems that at the end u answer your own question :) I did some quick test here and it seems to work for the `ACTION_PICK` but the `ACTION_GET_CONTENT` seems to still not pass along a file path. =/ – Budius Nov 25 '14 at 12:16
  • (I'should have +1 :D)Thank's really your answer was verry helpfull and great and for ACTION_GET_CONTENT I will check it also – Chlebta Nov 25 '14 at 13:34