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