We tried to solve this problem in the last few hours, finally decided that we should return to StackOverflow.
Here - We have an application that downloads a PDF file from the server to the application cache directory, and then opens it using the package manager. Of course, it goes through grantUriPermission()to provide read (and write) permissions to all available packages.
Despite the fact that it works fine on most devices, today we came across a device in which POLARIS Office 5 is installed as the default PDF viewer.
Whenever we open a file, Polaris simply displays the message "This document cannot be opened."
I have to say that when I try to open a file through Acrobat Reader it works fine. In addition, when we copied the file from the cache directory to an external directory (using the Android File Manager) and then opened it manually in Polaris, it worked fine.
We would refuse this, but since Polaris is the default viewer on many devices, we would really like to solve this problem.
Here is the code -
public void onDownloadDone(String filepath) {
File file = new File(filepath);
Intent intent = new Intent(Intent.ACTION_VIEW);
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(filepath);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
intent.setType(mimeType);
PackageManager pm = context.getPackageManager();
Uri contentUri = FileProvider.getUriForFile(context, "myapp.fileprovider", file);
intent.setData(contentUri);
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
for (ResolveInfo externalApp: activities){
String packageName = externalApp.activityInfo.packageName;
context.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
if (activities.size() > 0){
context.startActivity(intent);
} else {
System.out.println("Warning!!! No app for file " + filepath);
}
}
Thank you so much!
source
share