我试图打开放置在文件夹中的文件。这个想法是提供下载的应用程序可以打开的Office文件+ pdf的最大类型。因此,我下载了14种不同的文件类型示例,例如.docx,.ppt,.pdf等。我有WPS Office作为运行这些文件的应用程序。但是问题是当我使用下面的代码时,直接在WPS Office应用程序中打开文件。如果我更改为,target.setDataAndType(Uri.fromFile(open), "application/pdf");
则会显示Adobe Reader和WPS Office作为选项。有什么方法可以让我最好地选择打开文件而无需特别指定/ pdf或/ msword?这样我就可以根据输入类型获得应用列表。
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(open), "application/msword");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or
// something
Toast.makeText(
getApplicationContext(),
"Please install proper document reader in your device to open this file",
Toast.LENGTH_SHORT).show();
}
我假设您要使用文件扩展名打开应用程序,而不指定application / *。
File file = new File(filePath);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);
if (type == null)
type = "*/*";
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
intent.setDataAndType(data, type);
startActivity(intent);
这应该得到扩展名并打开“最佳可能”应用程序
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句