2

I am currently using the below code to open PDF files. When I try, it is not providing the URI. I am using with manage external storage.

    public Intent getFileChooser() {
    String folderPath = Environment.getExternalStorageDirectory() + "/";
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_GET_CONTENT);
    Uri myUri = Uri.parse(folderPath);
    intent.setDataAndType(myUri, mContext.getString(R.string.pdf_type));
    return Intent.createChooser(intent, mContext.getString(R.string.merge_file_select));
}

Below is the code I am using on activity result. This option is not providing the uri of the file.

mUri = data.getData();
        //Getting Absolute Path
        String path = RealPathUtil.getInstance().getRealPath(getContext(), data.getData());
        setTextAndActivateButtons(path);

Please help on fixing the issue.

Nourin
  • 23
  • 4
  • do you have `manage_external_storage` permission GRANTED? or just placed in manifest? – snachmsm Dec 16 '21 at 12:29
  • 1
    Delete `RealPathUtil`. There is no path, as `ACTION_GET_CONTENT` is not limited to the filesystem. – CommonsWare Dec 16 '21 at 13:04
  • @snachmsm it is just used in the manifest for now. – Nourin Dec 16 '21 at 16:04
  • @CommonsWare So what else should I be using in this case to get the uri? – Nourin Dec 16 '21 at 16:06
  • 1
    @Nourin you have to take manage_external_storage permission at runtime, declaring it on the manifest only won't work – DrHowdyDoo Dec 16 '21 at 16:08
  • @DrHowdyDoo Did you mean that I should be getting that permission as well, right? I already did that. I am getting prompt for that when I open the app for the first time and for now, the app will open only if the permission is provided. – Nourin Dec 16 '21 at 16:38
  • "So what else should I be using in this case to get the uri?" -- you already have the `Uri`. It is `data.getData()`. Use `ContentResolver` to work with it, such as using `openInputStream()` to read in the content identified by that `Uri`. – CommonsWare Dec 16 '21 at 17:23
  • @CommonsWare I have found out the issue. When the system filepicker shows up, it shows recent files and it doesn't provide the location of the file. But when I try going to the internal storage option in the system file picker and select the file, it works. Is there anything we can do to get from the recent list as well? – Nourin Dec 17 '21 at 04:51
  • Or else skipping the recent file altogether? – Nourin Dec 17 '21 at 05:11
  • `Did you mean that I should be getting that permission as well, ` Wrong info. You do not need that permission at all. – blackapps Dec 17 '21 at 08:08
  • `it shows recent files and it doesn't provide the location of the file` It is pretty unclear what you mean by that and for what you need a location. And it is the user of your app that selects a file. Not you. – blackapps Dec 17 '21 at 08:09
  • `going to the internal storage option in the system file picker and select the file, it works. ` It is unclear to me what would work and what would not work ik the user picked a file from Recent. – blackapps Dec 17 '21 at 08:11
  • "When the system filepicker shows up" -- it is not a "filepicker". It allows users to browse document trees and choose documents. Those documents do not have to be files on the local filesystem. For example, it could be Google Drive or a nearby SMB/CIFS file server. Or, it could be encrypted blobs in a database. "it doesn't provide the location of the file" -- that is because what you call the "system filepicker" is not limited to files. – CommonsWare Dec 17 '21 at 12:31
  • Is there anyway we can limited to files? @CommonsWare – Nourin Dec 19 '21 at 12:26
  • There is no way to limit `ACTION_GET_CONTENT` to files. – CommonsWare Dec 19 '21 at 12:29

0 Answers0