I am trying to select PDF file using intent action ACTION_OPEN_DOCUMENT on Android but its giving null on some devices.
I want the user to select PDF file only from his device. For this I have used intent action ACTION_OPEN_DOCUMENT.
This works correctly in most of the devices but on some of the devices mostly Xiaomi, on file selection it giving null on trying to get the intent data intent.getData() in onActivityResult using the option File Manager as shown in the image below.
I am trying to avoid using action ACTION_GET_CONTENT, since 3rd party apps doesn't restrict the content to pdf only. Other apps are allowing format which is not PDF as well.
In the image on selecting file from the menu File manager its giving null as intent.getData() but its working fine when i am selecting document from Documents option.
Below is the code for file chooser request
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
startActivityForResult(Intent.createChooser(intent, "Choose a file"), Constants.FILE_CHOOSER);
After file selection
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
final Uri uri = intent.getData(); //Uri is null
}
}
I am expecting some data in intent.getData() but its giving null.
Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=501, result=-1, data=Intent { (has extras) }} to activity {com.avail.easyloans.android/com.avail.easyloans.feature.cashup.activities.documents.ActivityUploadBankStmt}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getAuthority()' on a null object reference
What can be the possible error?