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

Time:05-10

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.

CodePudding user response:

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
}
  • Related