0

I have seen similar questions in SO here and here.

But still these didn't solve my issue, So I'm posting a new question.

Here is my code to get path of the image selected in Photos app. But I'm not getting the cursor.getColumnIndexOrThrow(column); value as 0 and cursor.getString(column_index) value as null.

Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }

Can anybody help me to solve this issue. If my question is too broad please let me know. I'll update my question.

UPDATE: Starting intent:

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, getString(R.string.selectPhoto)), RESULT_LOAD_IMAGE);

in onActivityResult:

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(contentURI, projection, null, null, null);
    if (cursor == null) {
          return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(projection[0]);
        return cursor.getString(idx);
    }
}

Regards

Community
  • 1
  • 1
ImGenie
  • 323
  • 1
  • 15

1 Answers1

0

User below code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Common.Action_Gallery:
                    if (data != null) {
                        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();                           
                    }
                    break;   
            }
        }
}
Tarun Gupta
  • 307
  • 2
  • 11
  • Still getting null at cursor.getString(columnIndex); call – ImGenie Aug 02 '16 at 07:18
  • My data.getData giving the response as "content://com.google.android.apps.photos.contentprovider/0/1/mediaKey%3A%2FAF1QipP_19ASFn4CF_8XmzBwXI4WRR9SSDTgR7ZopL2v/ACTUAL" – ImGenie Aug 02 '16 at 07:19
  • I believe you have not changed the code much as this code is working in my apps and tested with various devices. – Tarun Gupta Aug 02 '16 at 07:29
  • can u test this can u get the image from default photos app. select an image from photos option in the top and let me know please.? – ImGenie Aug 02 '16 at 09:08
  • I have tested it. I am able to get the image from the default photos app. Could you please share the new code that you are using to call intent and get result. – Tarun Gupta Aug 02 '16 at 10:22
  • Please share the code where you have assigned contentURI? – Tarun Gupta Aug 02 '16 at 10:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118890/discussion-between-tarun-gupta-and-imgenie). – Tarun Gupta Aug 02 '16 at 10:44