【发布时间】:2017-12-29 04:46:28
【问题描述】:
目前正在处理多种类型的文件,并允许它们在 android 手机中使用默认应用程序打开。 到目前为止,我可以打开所有其他文件,例如:pdf、图像、音频,并在手机中安装了默认应用程序。 但是当涉及到 xlxs、doc 等办公文件时。 已经安装了OFFICE SUITE,但我无法打开它们。
使用默认应用打开的代码:
@Override
public void onSelectedItem(String image_urls_, int position) {
String mimeType = getMimeType(image_urls_);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(image_urls_), mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("memtype", mimeType);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(Attachments.this, "No handler for this type of file.", Toast.LENGTH_LONG).show();
Log.e("bug_file", e.toString());
}
}
查找文件类型的代码
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type =
MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
【问题讨论】:
-
我认为您需要先下载文件,然后为 intent 分配本地路径。
-
请指导如何实现这一目标。 @ADM
-
你可以直接使用
DownloadManager进行下载。 -
好的,谢谢你会按照你说的尝试。 @ADM
-
您可以将它们显示在 WebView stackoverflow.com/a/12797706/2599596 中,而不是下载
标签: java android android-studio android-fileprovider