Dan*_*Dan 4 android android-package-managers
我们一直试图在过去几个小时内解决这个问题,最后决定我们应该恢复到StackOverflow.
接下来 - 我们有一个应用程序将PDF文件从服务器下载到应用程序的缓存目录,然后使用数据包管理器打开它.当然,它会grantUriPermission()为所有可用的包提供读(和写)权限.
虽然它在大多数设备上运行良好,但今天我们遇到了一个安装了POLARIS Office 5作为默认PDF查看器的设备.
每当我们打开文件时,Polaris只会显示一条消息"无法打开此文档".
我应该说,当试图通过Acrobat Reader打开文件时,它的效果很好.此外,当我们将文件从缓存目录复制到外部目录(使用Android的文件管理器),然后手动在Polaris中打开它时效果很好.
我们已经放弃了它,但由于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);
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢!
我有完全相同的问题.PDF文件将使用ezPDF和Adobe打开,但不会使用Polaris Viewer打开.您必须使用以下内容设置数据和类型:
intent.setDataAndType(uri, "application/pdf");
Run Code Online (Sandbox Code Playgroud)
而不是使用:
intent.setType("application/pdf");
intent.setData(uri);
Run Code Online (Sandbox Code Playgroud)
对我来说,现在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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5585 次 |
| 最近记录: |