日常开发中很多情况下会用到本地文件打开的功能,我们需要调用本地的应用(系统自带或者用户自己安装的)来打开文件,下边 我们总结了 一些常用的文件类型的打卡以及相关的封装。
代码如下:
public class FileOpener {
private static final String TAG = FileOpener.class.getSimpleName();
private Context context;
private String[] fileEndingWebText=new String[] {
".htm","html",".php",".jsp"
};
private String[] fileEndingText=new String[] {
".txt",".java",".c",".cpp",".py",".xml",".json",".log"
};
private String[] fileEndingWord=new String[] {
".doc",".docx"
};
private String[] fileEndingExcel=new String[] {
".xls",".xlsx"
};
private String[] fileEndingPPT=new String[] {
".ppt",".pptx"
};
private String[] fileEndingPdf=new String[] {
".pdf"
};
public FileOpener(Context context){
this.context = context;
}
public static Intent getHtmlFileIntent(File file)
{
Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
Intent intent = new Intent();
intent.setDataAndType(uri, "text/html");
intent.setAction("android.intent.action.DCSVIEW");
return intent;
}
public static Intent getImageFileIntent(File file)
{
Intent intent = new Intent();
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.intent.action.DCSVIEW");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "image/*");
return intent;
}
public static Intent getPdfFileIntent(File file)
{
Intent intent = new Intent();
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.intent.action.DCSVIEW");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
return intent;
}
public static Intent getTextFileIntent(File file)
{
Intent intent = new Intent();
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.intent.action.DCSVIEW");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "text/plain");
return intent;
}
public static Intent getAudioFileIntent(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "audio/*");
return intent;
}
public static Intent getVideoFileIntent(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "video/*");
return intent;
}
public static Intent getChmFileIntent(File file)
{
Intent intent = new Intent();
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.intent.action.DCSVIEW");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/x-chm");
return intent;
}
public static Intent getWordFileIntent(File file)
{
Intent intent = new Intent();
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.intent.action.DCSVIEW");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/msword");
return intent;
}
public static Intent getExcelFileIntent(File file)
{
Intent intent = new Intent();
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.intent.action.DCSVIEW");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.ms-excel");
return intent;
}
public static Intent getPPTFileIntent(File file)
{
Intent intent = new Intent();
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.intent.action.DCSVIEW");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
return intent;
}
public static Intent getApkFileIntent(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
return intent;
}
public boolean checkEndsWithInStringArray(String checkItsEnd,
String[] fileEndings){
for(String aEnd : fileEndings){
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
public void openFile(File currentPath) {
if(currentPath!=null&¤tPath.isFile())
{
String fileName = currentPath.toString();
Intent intent;
Resources r = context.getResources();
//Log.d(TAG, "R.array"+context.getResources().getStringArray(R.array.fileEndingImage));
if(checkEndsWithInStringArray(fileName, fileEndingWebText)){
intent = getHtmlFileIntent(currentPath);
context.startActivity(intent);
}else if(checkEndsWithInStringArray(fileName,fileEndingText)){
intent = getTextFileIntent(currentPath);
context.startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, fileEndingPdf)){
intent = getPdfFileIntent(currentPath);
context.startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, fileEndingWord)){
intent = getWordFileIntent(currentPath);
context.startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, fileEndingExcel)){
intent = getExcelFileIntent(currentPath);
context.startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, fileEndingPPT)){
intent = getPPTFileIntent(currentPath);
context.startActivity(intent);
}else{
Toast.makeText(context.getApplicationContext(), "Path:"+currentPath.getName(), Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(context.getApplicationContext(), "?", Toast.LENGTH_SHORT).show();
}
}
}
喜欢 就关注吧,欢迎投稿!
如有任何疑问可在文章底部留言。为了防止恶意评论,本博客现已开启留言审核功能。但是博主会在后台第一时间看到您的留言,并会在第一时间对您的留言进行回复!欢迎交流!
本文链接: https://leetcode.jp/关于文件打开的intent的终极总结/