1

I am struggling to access files in downloads folder that user can select.

Using the following to give user to select file

Intent intent;
    if (SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    } else {
        intent = new Intent(Intent.ACTION_GET_CONTENT);
    }

    // Filter to only show results that can be "opened", such as a
    // file (as opposed to a list of contacts or timezones)
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Filter to show only application/pdf, using the image MIME data type.
    intent.setType(PDF_MIME_TYPE);

    if (activity != null) {
        activity.startActivityForResult(intent, requestCode);
    }

As you might know then the following method is called when user selects the file

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 

(Expected): Please see the following I can get a decent path from selecting another folder I created "fromPC"(observe it is a file named:"815" and extension "PDF"):

enter image description here

(Problem): When doing the exact same code but selecting "Downloads" folder I only get "873" at the end but the file name I selected was "PaymentNotification" and extension "PDF":

enter image description here

Reading online but nothing quite as specific as this.

So I am not sure how to let user select a file example PDF from "Downloads" folder after the scope storage change in new Android thanks in advance for the answer?

If there are a way to get a "File"(java.io) from within the "onActivityResult" method it would also solve my problem?

  • 1
    "I can get a decent path" -- the path is meaningless; please ignore it. "If there are a way to get a "File"(java.io) from within the "onActivityResult" method" -- no, because you are not working with files. Use `ContentResolver` and `DocumentFile` to work with the documents that you get back from those `Intent` actions. – CommonsWare Oct 06 '21 at 15:50
  • @CommonsWare I'm not getting RESULT CODE 0 on Android 11 when the intent is fired with OPEN DOCUMENT flag. – Zaid Mirza Feb 02 '22 at 07:50
  • 1
    @ZaidMirza: `ACTION_OPEN_DOCUMENT` is an `Intent` action, not a flag. – CommonsWare Feb 02 '22 at 11:42
  • @CommonsWare my Bad, Yes. The issue is resolved. I had to implement ActivityResultAPI instead onActvityResult https://stackoverflow.com/questions/70952359/onactivityresult-always-returns-result-canceled-code-0-on-android-11/70952504#70952504 – Zaid Mirza Feb 02 '22 at 12:24

0 Answers0