Raman Branavitski
Raman Branavitski

Reputation: 894

Get image path in Android

When i pick some photos from gallery, they have next uri

content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh3.googleusercontent.com%2F1cSCAm2mqyFgOUPR8d_8Modz0Cgu6yn1nsznrhfzQTw%3Ds0-d

instead of something like

content://media/external/images/media/3728

and i cant get path to this image. How can i resolve this problem?

My code:

case RC_SELECT_PICTURE:
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getActivity().getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
            cursor.close();
            break;

I try to get filePath using some methods which I found on StackOverflow, but this path is url. I can download my image using it. I need to get local filePath.

Upvotes: 1

Views: 2586

Answers (2)

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this may hepls you

Check Android SDK version when you go for pick image from gallery.

if (Build.VERSION.SDK_INT <19){
    Intent intent = new Intent(); 
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
} else {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
}

Upvotes: -1

CommonsWare
CommonsWare

Reputation: 1006554

A Uri has never had to map to a File path.

Now, it turns out that MediaStore leaked paths, and a few idiots promoted the pattern of using those leaks, leading lots of developers to think that this was reliable behavior. It wasn't. A ContentProvider can serve up all sorts of streams, not backed by a file that you have access to, such as having the file be on internal storage.

If you want the content of the image pointed to by this Uri, or you want to know its MIME type, use a ContentResolver.

Upvotes: 2

Related Questions