使用北极星办公室 5 在外部打开 PDF 返回"This document cannot be opened"



在过去的几个小时里,我们一直在努力解决这个问题,最终决定恢复到StackOverflow。

给-我们有一个应用程序,它可以将PDF文件从服务器下载到应用程序的缓存目录,然后使用数据包管理器打开它。当然,它要通过grantUriPermission()来授予所有可用包的读(和写)权限。

尽管它在大多数设备上都能很好地工作,但今天我们遇到了一款安装了POLARIS Office 5作为默认PDF查看器的设备。

每当我们打开文件时,Polaris只会显示一条消息,上面写着"无法打开此文档"。

我应该说,当试图通过AcrobatReader打开文件时,效果很好。此外,当我们将文件从缓存目录复制到外部目录(使用Android的文件管理器),然后在Polaris中手动打开它时,效果非常好。

我们本可以放弃它,但由于北极星是许多设备上的默认查看器,我们真的很想解决这个问题。

这是代码-

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);
}
}

非常感谢!

我遇到了完全相同的问题。PDF文件将使用ezPDFAdobe打开,但不使用Polaris Viewer。您必须设置数据并键入:

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

而不是使用:

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);

最新更新