android intent打开各种文件(txt、apk、ppt,面试官不讲武德

intent = FileUtils.openFile(this,filePath);

startActivity(intent);

break;

case R.id.button_xls:

filePath = “/storage/emulated/0/新建 XLS 工作表.xls”;

intent = FileUtils.openFile(this,filePath);

startActivity(intent);

break;

case R.id.button_doc:

filePath = “/storage/emulated/0/新建 DOC 文档.doc”;

intent = FileUtils.openFile(this,filePath);

startActivity(intent);

break;

}

}

}

2.FileUtils.java中:

public class FileUtils {

//打开类型的文件

public static Intent openFile(Context context, String filePath) {

if (filePath == null) {

return null;

}

File file = new File(filePath);

if (!file.exists())

return null;

/* 取得扩展名 */

String end = file

.getName()

.substring(file.getName().lastIndexOf(".") + 1,

file.getName().length()).toLowerCase();

end = end.trim().toLowerCase();

// System.out.println(end);

/* 依扩展名的类型决定MimeType */

if (end.equals(“apk”)) {

return getApkFileIntent(c

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

ontext,filePath);

} else if (end.equals(“ppt”)) {

return getPptFileIntent(context,filePath);

} else if (end.equals(“xls”)) {

return getExcelFileIntent(context,filePath);

} else if (end.equals(“doc”)) {

return getWordFileIntent(context,filePath);

} else if (end.equals(“pdf”)) {

return getPdfFileIntent(context,filePath);

} else if (end.equals(“txt”)) {

return getTextFileIntent(context,filePath, false);

} else {

return getAllIntent(context,filePath);

}

}

// Android获取一个用于打开APK文件的intent

public static Intent getApkFileIntent(Context context,String param) {

Intent intent = new Intent(Intent.ACTION_VIEW);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Uri contentUri = FileProvider.getUriForFile(context, “com.filepath.intent.fileprovider”, new File(param));

intent.setDataAndType(contentUri, “application/vnd.android.package-archive”);

} else {

intent.setDataAndType(Uri.fromFile(new File(param)), “application/vnd.android.package-archive”);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

}

return intent;

}

// Android获取一个用于打开PPT文件的intent

public static Intent getPptFileIntent(Context context,String param) {

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(new File(param));

Uri uri = FileProvider.getUriForFile(context, “com.filepath.intent.fileprovider”, new File(param));

intent.setDataAndType(uri, “application/vnd.ms-powerpoint”);

return intent;

}

// Android获取一个用于打开Excel文件的intent

public static Intent getExcelFileIntent(Context context,String param) {

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(new File(param));

Uri uri = FileProvider.getUriForFile(context, “com.filepath.intent.fileprovider”, new File(param));

intent.setDataAndType(uri, “application/vnd.ms-excel”);

return intent;

}

// Android获取一个用于打开Word文件的intent

public static Intent getWordFileIntent(Context context,String param) {

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(new File(param));

Uri uri = FileProvider.getUriForFile(context, “com.filepath.intent.fileprovider”, new File(param));

intent.setDataAndType(uri, “application/msword”);

return intent;

}

// Android获取一个用于打开PDF文件的intent

public static Intent getPdfFileIntent(Context context,String param) {

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(new File(param));

Uri uri = FileProvider.getUriForFile(context, “com.filepath.intent.fileprovider”, new File(param));

intent.setDataAndType(uri, “application/pdf”);

return intent;

}

// Android获取一个用于打开文本文件的intent

public static Intent getTextFileIntent(Context context,String param, boolean paramBoolean) {

Intent intent = new Intent(“android.intent.action.VIEW”);

intent.addCategory(“android.intent.category.DEFAULT”);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if (paramBoolean) {

Uri uri1 = Uri.parse(param);

intent.setDataAndType(uri1, “text/plain”);

} else {

// Uri uri2 = Uri.fromFile(new File(param));

Uri uri2 = FileProvider.getUriForFile(context, “com.filepath.intent.fileprovider”, new File(param));

intent.setDataAndType(uri2, “text/plain”);

}

return intent;

}

// Android获取一个用于打开APK文件的intent

public static Intent getAllIntent(Context context,String param) {

Intent intent = new Intent();

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.setAction(android.content.Intent.ACTION_VIEW);

// Uri uri = Uri.fromFile(new File(param));

Uri uri = FileProvider.getUriForFile(context, “com.filepath.intent.fileprovider”, new File(param));

intent.setDataAndType(uri, “/”);

return intent;

}

}

3.fileprovider.java中

上一篇:URL和URI


下一篇:Hadoop Hdfs常用命令