[英]intent.resolveActivity returns null on android.intent.action.OPEN_DOCUMENT in API 30
I am having problem to pick a pdf file.我在选择 pdf 文件时遇到问题。 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.之前,targetSdk 是 29 并且工作正常,但是当我将其更新到 30 时,Android 11 及更高版本的设备无法打开文件管理器来选择 pdf 文件。 Device with version below Android 11 is working fine.版本低于 Android 11 的设备运行良好。 This is the code to create the Intent:这是创建 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.在调试时,intent.resolveActivity 在 Android 11 及更高版本上返回 null。
in Android 11 were made some changes in package visbility, some info HERE and HERE在 Android 11 中对包的可见性进行了一些更改,一些信息HERE和HERE
note that resolveActivity
should return package name of app, which can handle this intent.请注意, resolveActivity
应该返回可以处理此意图的应用程序的包名称。 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){}
而是尝试在没有if
的情况下严格运行startActivityForResult
并将其包装在try { ... } catch (ActivityNotFoundException ex){}
boolean canBeLaunched;
try {
fragment.startActivityForResult(intent, requestCode);
canBeLaunched = true
} catch (ActivityNotFoundException ex){
canBeLaunched = false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.