简体   繁体   中英

intent.resolveActivity returns null on android.intent.action.OPEN_DOCUMENT in API 30

I am having problem to pick a pdf file. Before, the targetSdk is 29 and it's working fine but when I updated it to 30, device with Android 11 and above can't open the file manager to pick the pdf file. Device with version below Android 11 is working fine. This is the code to create the Intent:

Intent intent = new Intent("android.intent.action.OPEN_DOCUMENT");
intent.addCategory("android.intent.category.OPENABLE");
intent.setType("application/pdf");

and this is the code to launch the intent :

boolean canBeLaunched = false;
if (intent.resolveActivity(fragment.getActivity().getPackageManager()) != null) {
    canBeLaunched = true;
    fragment.startActivityForResult(intent, requestCode);
}

On debug, intent.resolveActivity returns null on Android 11 and above.

in Android 11 were made some changes in package visbility, some info HERE and HERE

note that resolveActivity should return package name of app, which can handle this intent. Thats against freshly introduced rules and new policy as above

instead try to run startActivityForResult strictly without your if and wrap it in try { ... } catch (ActivityNotFoundException ex){}

boolean canBeLaunched;
try {
    fragment.startActivityForResult(intent, requestCode);
    canBeLaunched = true
} catch (ActivityNotFoundException ex){
    canBeLaunched = false
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM