Search code examples
androidandroid-intentandroid-gallery

Getting image real path from gallary using android api 23 on nexus emulator


This is how I open gallary

Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);

This is my onActvitityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Disc.setText(selectedImagePath);
            }
        }
    }

This is how im trying to get the real path from gallary but its returning nothing, String its returning is empty.

private String getPath(Uri uri) {
        // just some safety built in
        if( uri == null ) {
            Toast.makeText(this, "Error problem with system", Toast.LENGTH_SHORT).show();
            return null;
        }
        String path = "No-Path-Found";
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if(cursor.moveToFirst()){
            int column_index = cursor
                    .getColumnIndex(MediaStore.Images.Media.DATA);
            path = cursor.getString(column_index);
        }
            cursor.close();
            return path;
    }

Can anyone help me with this, I been searching online but nothing is working


Solution

  • Found out that you need to use runtime permision for android api 19 and above, it works perfectly now:)