0

I'm developing an app where I select a .pdf file from the storage, it works when I try it on versions of android 5.1 and 6, but then I try on android 8 and it does not get the file, it does not pop the app just does not show it. How can I make it work on android 8 as in versions 5.1 or 6 ?. The user does grant external storage permits

private void checkStoragePermission() {
    if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        selectDocument();
    } else {
        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
    }
}

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_PERMISSION_STORAGE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            selectDocument();
       }  else {
            Toast.makeText(getContext(), "Por favor conceda el permiso.", Toast.LENGTH_SHORT).show();
        }
    }

private void selectDocument() {
        Intent intent = new Intent();
        intent.setType("application/pdf");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, REQUEST_CODE_DOCUMENT);
    }

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_DOCUMENT && resultCode == Activity.RESULT_OK && data != null) {
            Uri selectedFileUri = data.getData();
            selectedDocumentPath = FilePath.getPath(getContext(),selectedFileUri);
            if (selectedDocumentPath != null && !selectedDocumentPath.equals("")){
                edtSymptomFile.setText(selectedFileUri.getPath());
            }
        }
    }
jeanf
  • 365
  • 1
  • 3
  • 12
  • See also [this](https://stackoverflow.com/questions/49221312/android-get-real-path-of-a-txt-file-selected-from-the-file-explorer) and [this](https://stackoverflow.com/questions/35870825/getting-the-absolute-file-path-from-content-uri-for-searched-images). – CommonsWare Nov 10 '18 at 20:40
  • Those questions do not have to do with my problem showing files in android 8, therefore it can not be marked as a duplicate. – jeanf Nov 10 '18 at 21:05
  • 1
    "Those questions do not have to do with my problem showing files in android 8" -- yes, they do. You are trying to get a file path from a `Uri`. All three of those questions explain, in detail, how that is not going to work. – CommonsWare Nov 10 '18 at 21:07

0 Answers0