Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oluwabajio/390e3ba86eccf448f7aa773623a88323 to your computer and use it in GitHub Desktop.
Save oluwabajio/390e3ba86eccf448f7aa773623a88323 to your computer and use it in GitHub Desktop.
public void selectImage() {
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, 411);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode){
case 411 : {
Log.e(TAG, "onActivityResult: uri = "+data.getData().toString() );
Bundle bundle = new Bundle();
bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, 20);
bundle.putInt(ContentResolver.QUERY_ARG_OFFSET, 0);
bundle.putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, MediaStore.MediaColumns.DATE_MODIFIED+" DESC");
Cursor returnCursor = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
returnCursor = getActivity().getContentResolver().query(data.getData(), new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA}, bundle, null);
if (returnCursor.moveToFirst()) {
returnCursor.move(0);
Log.e(TAG, "onActivityResult: real path = " + returnCursor.getString(returnCursor.getColumnIndex(MediaStore.Images.Media.DATA)));
}
}
Log.e(TAG, "onActivityResult: "+ getActivity().getContentResolver().getType(data.getData()) );
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment