Android 打开PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO 格式文件代码

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.net.Uri;

importandroid.content.Intent;

//PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO

示例:

//这个不行,可能是因为PDF.apk程序没有权限访问其它APK里的asset资源文件,又或者是路径写错?

//Intentit=getPdfFileIntent("file:///android_asset/helphelp.pdf");

//下面这些都OK

//Intentit=getHtmlFileIntent("/mnt/sdcard/tutorial.html");//SD卡主目录

//Intentit=getHtmlFileIntent("/sdcard/tutorial.html");//SD卡主目录,这样也可以

Intentit=getHtmlFileIntent("/system/etc/tutorial.html");//系统内部的etc目录

//Intentit=getPdfFileIntent("/system/etc/helphelp.pdf");

//Intentit=getWordFileIntent("/system/etc/help.doc");

//Intentit=getExcelFileIntent("/mnt/sdcard/Book1.xls")

//Intentit=getPptFileIntent("/mnt/sdcard/download/Android_PPT.ppt");//SD卡的download目录下

//Intentit=getVideoFileIntent("/mnt/sdcard/ice.avi");

//Intentit=getAudioFileIntent("/mnt/sdcard/ren.mp3");

//Intentit=getImageFileIntent("/mnt/sdcard/images/001041580.jpg");

//Intentit=getTextFileIntent("/mnt/sdcard/hello.txt",false);

startActivity(it);

publicclassMyIntent

{

//android获取一个用于打开HTML文件的intent

publicstaticIntentgetHtmlFileIntent(Stringparam)

{

Uriuri=Uri.parse(param).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param).build();

Intentintent=newIntent("android.intent.action.VIEW");

intent.setDataAndType(uri,"text/html");

returnintent;

}

//android获取一个用于打开图片文件的intent

publicstaticIntentgetImageFileIntent(Stringparam)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Uriuri=Uri.fromFile(newFile(param));

intent.setDataAndType(uri,"image/*");

returnintent;

}

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

publicstaticIntentgetPdfFileIntent(Stringparam)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Uriuri=Uri.fromFile(newFile(param));

intent.setDataAndType(uri,"application/pdf");

returnintent;

}

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

publicstaticIntentgetTextFileIntent(Stringparam,booleanparamBoolean)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if(paramBoolean)

{

Uriuri1=Uri.parse(param);

intent.setDataAndType(uri1,"text/plain");

}

else

{

Uriuri2=Uri.fromFile(newFile(param));

intent.setDataAndType(uri2,"text/plain");

}

returnintent;

}

//android获取一个用于打开音频文件的intent

publicstaticIntentgetAudioFileIntent(Stringparam)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent.putExtra("oneshot",0);

intent.putExtra("configchange",0);

Uriuri=Uri.fromFile(newFile(param));

intent.setDataAndType(uri,"audio/*");

returnintent;

}

//android获取一个用于打开视频文件的intent

publicstaticIntentgetVideoFileIntent(Stringparam)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent.putExtra("oneshot",0);

intent.putExtra("configchange",0);

Uriuri=Uri.fromFile(newFile(param));

intent.setDataAndType(uri,"video/*");

returnintent;

}

//android获取一个用于打开CHM文件的intent

publicstaticIntentgetChmFileIntent(Stringparam)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Uriuri=Uri.fromFile(newFile(param));

intent.setDataAndType(uri,"application/x-chm");

returnintent;

}

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

publicstaticIntentgetWordFileIntent(Stringparam)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Uriuri=Uri.fromFile(newFile(param));

intent.setDataAndType(uri,"application/msword");

returnintent;

}

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

publicstaticIntentgetExcelFileIntent(Stringparam)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Uriuri=Uri.fromFile(newFile(param));

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

returnintent;

}

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

publicstaticIntentgetPptFileIntent(Stringparam)

{

Intentintent=newIntent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Uriuri=Uri.fromFile(newFile(param));

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

returnintent;

}

}

相关推荐