Opening PDF externally with Polaris office 5 returns "This document cannot be opened"

StackOverflow https://stackoverflow.com/questions/21662967

  •  09-10-2022
  •  | 
  •  

سؤال

We've been trying to solve this issue for the past few hours, finally decided we should revert to StackOverflow.

Here goes - We have an application that downloads a PDF file from a server to the app's cache directory and then opens it using the packet manager. Of course it goes through grantUriPermission() to give read (and write) permissions to all available packages.

Though it works great on most devices, today we encountered a device that has POLARIS Office 5 installed as the default PDF viewer.

Whenever we're opening the file, Polaris simply displays a message saying "This document cannot be opened".

I should say that when trying to open the file through Acrobat Reader, it works great. Also, when we copied the file from the cache directory to an external directory (using Android's File manager) and then opened it in Polaris manually it worked great.

We would have given up on it, but since Polaris is the default viewer on many devices, we really would love solving that 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's 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 a lot!

هل كانت مفيدة؟

المحلول

I had exactly the same problem. The PDF files would open with ezPDF and Adobe but not with Polaris Viewer. You have to set the data and type with:

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

instead of using:

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

For me the following is working fine with Polaris now:

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top