admin 管理员组文章数量: 887201
private Map<String, String> getMimeMap() {
Map<String, String> mapSimple = new HashMap<>();
if (mapSimple.size() == 0) {
mapSimple.put(".3gp", "video/3gpp");
mapSimple.put(".apk", "application/vnd.android.package-archive");
mapSimple.put(".asf", "video/x-ms-asf");
mapSimple.put(".avi", "video/x-msvideo");
mapSimple.put(".bin", "application/octet-stream");
mapSimple.put(".bmp", "image/bmp");
mapSimple.put(".c", "text/plain");
mapSimple.put(".chm", "application/x-chm");
mapSimple.put(".class", "application/octet-stream");
mapSimple.put(".conf", "text/plain");
mapSimple.put(".cpp", "text/plain");
mapSimple.put(".doc", "application/msword");
mapSimple.put(".docx", "application/msword");
mapSimple.put(".exe", "application/octet-stream");
mapSimple.put(".gif", "image/gif");
mapSimple.put(".gtar", "application/x-gtar");
mapSimple.put(".gz", "application/x-gzip");
mapSimple.put(".h", "text/plain");
mapSimple.put(".htm", "text/html");
mapSimple.put(".html", "text/html");
mapSimple.put(".jar", "application/java-archive");
mapSimple.put(".java", "text/plain");
mapSimple.put(".jpeg", "image/jpeg");
mapSimple.put(".jpg", "image/jpeg");
mapSimple.put(".js", "application/x-javascript");
mapSimple.put(".log", "text/plain");
mapSimple.put(".m3u", "audio/x-mpegurl");
mapSimple.put(".m4a", "audio/mp4a-latm");
mapSimple.put(".m4b", "audio/mp4a-latm");
mapSimple.put(".m4p", "audio/mp4a-latm");
mapSimple.put(".m4u", "video/vnd.mpegurl");
mapSimple.put(".m4v", "video/x-m4v");
mapSimple.put(".mov", "video/quicktime");
mapSimple.put(".mp2", "audio/x-mpeg");
mapSimple.put(".mp3", "audio/x-mpeg");
mapSimple.put(".mp4", "video/mp4");
mapSimple.put(".mpc", "application/vnd.mpohun.certificate");
mapSimple.put(".mpe", "video/mpeg");
mapSimple.put(".mpeg", "video/mpeg");
mapSimple.put(".mpg", "video/mpeg");
mapSimple.put(".mpg4", "video/mp4");
mapSimple.put(".mpga", "audio/mpeg");
mapSimple.put(".msg", "application/vnd.ms-outlook");
mapSimple.put(".ogg", "audio/ogg");
mapSimple.put(".pdf", "application/pdf");
mapSimple.put(".png", "image/png");
mapSimple.put(".pps", "application/vnd.ms-powerpoint");
mapSimple.put(".ppt", "application/vnd.ms-powerpoint");
mapSimple.put(".pptx", "application/vnd.ms-powerpoint");
mapSimple.put(".prop", "text/plain");
mapSimple.put(".rar", "application/x-rar-compressed");
mapSimple.put(".rc", "text/plain");
mapSimple.put(".rmvb", "audio/x-pn-realaudio");
mapSimple.put(".rtf", "application/rtf");
mapSimple.put(".sh", "text/plain");
mapSimple.put(".tar", "application/x-tar");
mapSimple.put(".tgz", "application/x-compressed");
mapSimple.put(".txt", "text/plain");
mapSimple.put(".wav", "audio/x-wav");
mapSimple.put(".wma", "audio/x-ms-wma");
mapSimple.put(".wmv", "audio/x-ms-wmv");
mapSimple.put(".wps", "application/vnd.ms-works");
mapSimple.put(".xml", "text/plain");
mapSimple.put(".xls", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
mapSimple.put(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
mapSimple.put(".z", "application/x-compress");
mapSimple.put(".zip", "application/zip");
mapSimple.put("", "*/*");
}
return mapSimple;
}
//得到file的类型
private String getMimeTypeFromFile(File file) {
String type = "*/*";
String fName = file.getName();
//获取后缀名前的分隔符"."在fName中的位置。
int dotIndex = fName.lastIndexOf(".");
if (dotIndex > 0) {
//获取文件的后缀名
String end = fName.substring(dotIndex).toLowerCase(Locale.getDefault());
//在MIME和文件类型的匹配表中找到对应的MIME类型。
Map<String, String> map = getMimeMap();
if (!TextUtils.isEmpty(end) && map.keySet().contains(end)) {
type = map.get(end);
}
}
return type;
}
有些文件种类可能不对了,我们可以先查看要打开文件的文件种类(找到文件,查看详情,找到文件种类就可以了),然后自行更改一下
import android.content.Intent;
import android.Uri;
import java.io.File;
public class OpenFiles {
//android获取一个用于打开HTML文件的intent
public static Intent getHtmlFileIntent(String Path)
{
File file = new File(Path);
Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(uri, getMimeTypeFromFile(file));//"text/html"
return intent;
}
//android获取一个用于打开图片文件的intent
public static Intent getImageFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, getMimeTypeFromFile(file));//"image/*"
return intent;
}
//android获取一个用于打开PDF文件的intent
public static Intent getPdfFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri,getMimeTypeFromFile(file));// "application/pdf"
return intent;
}
//android获取一个用于打开文本文件的intent
public static Intent getTextFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, getMimeTypeFromFile(file));//"text/plain"
return intent;
}
//android获取一个用于打开音频文件的intent
public static Intent getAudioFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, getMimeTypeFromFile(file));//"audio/*"
return intent;
}
//android获取一个用于打开视频文件的intent
public static Intent getVideoFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri,getMimeTypeFromFile(file));//"video/*"
return intent;
}
//android获取一个用于打开CHM文件的intent
public static Intent getChmFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, getMimeTypeFromFile(file));//"application/x-chm"
return intent;
}
//android获取一个用于打开Word文件的intent
public static Intent getWordFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, getMimeTypeFromFile(file));//"application/msword"
return intent;
}
//android获取一个用于打开Excel文件的intent
public static Intent getExcelFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, getMimeTypeFromFile(file));
return intent;
}
//android获取一个用于打开PPT文件的intent
public static Intent getPPTFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, getMimeTypeFromFile(file));//"application/vnd.ms-powerpoint"
return intent;
}
//android获取一个用于打开apk文件的intent
public static Intent getApkFileIntent(String Path)
{
File file = new File(Path);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), getMimeTypeFromFile(file));//"application/vnd.android.package-archive"
return intent;
}
}
本文标签: 第三方软件 文件 doc Android text
版权声明:本文标题:android使用第三方软件打开本地doc、pdf、ppt、text等文件 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1734992007h1687719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论