Android: Cannot get file path onActivityResult

Viewed 1435

Intent to select file:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("*/*");
            startActivityForResult(intent, FILE_REQUEST_CODE);

onActivityResultCode:

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FILE_REQUEST_CODE:
                if (resultCode == RESULT_OK) {
          Uri uri = data.getData();
                assert uri != null;
                String uriString = uri.toString();
                File myFile = new File(uri.getPath());
                String path = getPathFromUri(uri);         

    }
   }

}

The code to get the path is

private String getPathFromUri(Uri uri) {
        String[] filePathColumn = { MediaStore.Files.FileColumns.DATA };
        Cursor cursor = getContentResolver().query(uri,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String Path = cursor.getString(columnIndex);
        cursor.close();
        return Path;
    }

The path is returned as null, I have tried alot of things file.getPath() does not work either...what am I missing

0 Answers
Related