如何在Android上打开所有格式文件

时间:2012-06-29 01:09:46

标签: android android-intent

我尝试实现类似浏览器的应用 我想让它可以打开设备支持的任何格式文件 我知道下面的代码可以打开特定的格式:

String mimetype = mime_type(FileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(File), mimetype);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

public String mime_type(String name) {
String type = null;
String[] mime = {".htm\ttext/html", ".html\ttext/html", ".doc\tapplication/msword", ".ppt\tapplication/vnd.ms-powerpoint", ".xls\tapplication/vnd.ms-excel", 
                ".txt\ttext/plain", ".pdf\tapplication/pdf", ".xlsx\tapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
                ".pptx\tapplication/vnd.openxmlformats-officedocument.presentationml.presentation", ".docx\tapplication/vnd.openxmlformats-officedocument.wordprocessingml.document"};
int i;
for(i = 0; i < mime.length; i++) {
if(name.toLowerCase().endsWith(mime[i].split("\t")[0])) {
return mime[i].split("\t")[1];
}
}
return type;
}

但文件格式超出了我的清单 如果有任何方法可以为所有格式执行此操作? 或列出所有应用程序的任何方法都允许用户选择?

1 个答案:

答案 0 :(得分:0)

我发现设置mimetype到*/*可以到达它 它将显示所有已安装的应用程序以供选择。