android用于打开各种文件的intent(转)

0
回复
98106
查看
[复制链接]

223

主题

229

帖子

941

安币

代码手工艺人

Rank: 4

发表于 2011-5-27 19:14:36 | 显示全部楼层 |阅读模式
  1. import android.app.Activity;

  2. import android.content.Intent;

  3. import android.net.Uri;

  4. import android.net.Uri.Builder;

  5. import java.io.File;

  6. import android.content.Intent;

  7. //自定义android Intent类,

  8. //可用于获取打开以下文件的intent

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

  10. 示例:

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

  12. //Intent it = getPdfFileIntent("file:///android_asset/helphelp.pdf");

  13. //下面这些都OK

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

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

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

  17. //Intent it = getPdfFileIntent("/system/etc/helphelp.pdf");

  18. //Intent it = getWordFileIntent("/system/etc/help.doc");

  19. //Intent it = getExcelFileIntent("/mnt/sdcard/Book1.xls")

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

  21. //Intent it = getVideoFileIntent("/mnt/sdcard/ice.avi");

  22. //Intent it = getAudioFileIntent("/mnt/sdcard/ren.mp3");

  23. //Intent it = getImageFileIntent("/mnt/sdcard/images/001041580.jpg");

  24. //Intent it = getTextFileIntent("/mnt/sdcard/hello.txt",false);

  25. startActivity( it );

  26. public class MyIntent

  27. {

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

  29. public static Intent getHtmlFileIntent( String param )

  30. {

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

  32. Intent intent = new Intent("android.intent.action.VIEW");

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

  34. return intent;

  35. }

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

  37. public static Intent getImageFileIntent( String param )

  38. {

  39. Intent intent = new Intent("android.intent.action.VIEW");

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

  41. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

  44. return intent;

  45. }

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

  47. public static Intent getPdfFileIntent( String param )

  48. {

  49. Intent intent = new Intent("android.intent.action.VIEW");

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

  51. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

  54. return intent;

  55. }

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

  57. public static Intent getTextFileIntent( String param, boolean paramBoolean)





  58. {



  59. Intent intent = new Intent("android.intent.action.VIEW");



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



  61. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);



  62. if (paramBoolean)



  63. {



  64. Uri uri1 = Uri.parse(param );



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





  66. }



  67. else



  68. {







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



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



  71. }

  72. return intent;



  73. }

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

  75. public static Intent getAudioFileIntent( String param )

  76. {

  77. Intent intent = new Intent("android.intent.action.VIEW");

  78. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

  79. intent.putExtra("oneshot", 0);

  80. intent.putExtra("configchange", 0);

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

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

  83. return intent;

  84. }

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

  86. public static Intent getVideoFileIntent( String param )

  87. {

  88. Intent intent = new Intent("android.intent.action.VIEW");

  89. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

  90. intent.putExtra("oneshot", 0);

  91. intent.putExtra("configchange", 0);

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

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

  94. return intent;

  95. }


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

  97. public static Intent getChmFileIntent( String param )

  98. {

  99. Intent intent = new Intent("android.intent.action.VIEW");

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

  101. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

  104. return intent;

  105. }


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

  107. public static Intent getWordFileIntent( String param )

  108. {

  109. Intent intent = new Intent("android.intent.action.VIEW");

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

  111. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

  114. return intent;

  115. }

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

  117. public static Intent getExcelFileIntent( String param )

  118. {

  119. Intent intent = new Intent("android.intent.action.VIEW");

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

  121. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

  124. return intent;

  125. }

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

  127. public static Intent getPptFileIntent( String param )

  128. {

  129. Intent intent = new Intent("android.intent.action.VIEW");

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

  131. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

  134. return intent;

  135. }

  136. }
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )