Opening PDF from the outside with Polaris office 5 returns "This document does not open"

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) {
        // Set a file object that represents the downloaded file
        File file = new File(filepath);
        // Set an intent for the external app
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Get mime type of the downloaded file
        String fileExtension = MimeTypeMap.getFileExtensionFromUrl(filepath);
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
        intent.setType(mimeType);

        // Look for installed packges according to the file mime type
        PackageManager pm = context.getPackageManager();

        Uri contentUri = FileProvider.getUriForFile(context, "myapp.fileprovider", file);
        // Set the file uri in the intent
        intent.setData(contentUri);

        // Give permissions to the file to each external app that can open the file
        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);
        }

        // Start the activities (or launch the menu) if any activity exists
        if (activities.size() > 0){
            context.startActivity(intent);
        } else {
            System.out.println("Warning!!! No app for file " + filepath);
        }

    }

Thank you so much!

+3
source share
1 answer

I had exactly the same problem. PDF files will open with ezPDF and Adobe , but not with Polaris Viewer . You must set the data and enter:

intent.setDataAndType(uri, "application/pdf");

instead of using:

intent.setType("application/pdf");
intent.setData(uri);

Polaris:

Uri uri = FileProvider.getUriForFile(MyApplication.getContext(), MyApplication.getContext().getPackageName() + ".myfileprovider", destFile);

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
+5

All Articles