`
test_angel
  • 浏览: 49977 次
  • 性别: Icon_minigender_2
  • 来自: 西安
社区版块
存档分类
最新评论

Intent(open file) android

 
阅读更多

这周我就写写如何在android上打开各种文件的方法(前提:你安装了相应文件的阅读器,eg:pdf)

 

step1:我们将文件的后缀和相应的datatype存入hashmap中,例如:

			HashMap<String , String> mTypeMap = new HashMap<String, String>();
			mTypeMap.put("pdf", "application/pdf");
			mTypeMap.put("rar", "application/rar");
			mTypeMap.put("doc", "application/msword");
			mTypeMap.put("mp3", "audio/mpeg");

 

step2:根据文件名获取文件后缀:eg.  pdf

 

step3:根据后缀名获取datatype:eg. pdf  ---------->application/pdf,假设其方法名为:getMineType(String name)

 

step4:new Intent,并将文件的路径传递给准备跳转的activity:

String action = "android.intent.action.VIEW";
Intent intent = new Intent();
intent.setAction(action);
intent.setDataAndType(uri, getMineType("pdf"));
startActivity(intent);

 

 

 

注:以下是一些后缀对应的datatype,可以参考参考:

"application/ogg", "ogg"
"application/pdf", "pdf"
"application/rar", "rar"
"application/rdf+xml", "rdf"
"application/rss+xml", "rss"
"application/zip", "zip"
"application/vnd.android.package-archive",  "apk"
"application/vnd.oasis.opendocument.database", "odb"
"application/vnd.oasis.opendocument.formula", "odf"
"application/vnd.oasis.opendocument.graphics", "odg"
"application/vnd.oasis.opendocument.spreadsheet", "ods"
"application/vnd.oasis.opendocument.spreadsheet-template","ots"
"application/vnd.oasis.opendocument.text", "odt"
"application/vnd.oasis.opendocument.text-master", "odm"
"application/msword", "doc"
"application/msword", "dot"
"application/vnd.openxmlformats-officedocument.wordprocessingml.document","docx"
"application/vnd.ms-excel", "xls"
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","xlsx"
"application/vnd.ms-powerpoint", "ppt"
"application/vnd.openxmlformats-officedocument.presentationml.presentation","pptx");
"application/vnd.visio", "vsd"
"audio/3gpp", "3gpp"
 "audio/mpeg", "mp3"
 "audio/x-ms-wma", "wma"
 "audio/x-pn-realaudio", "rm"
 "image/bmp", "bmp"
  "image/gif", "gif"
 "image/ico", "cur"
"image/ico", "ico"
  "image/x-ms-bmp", "bmp"
  "text/css", "css"
 "text/html", "htm"
"text/html", "html"
   "text/plain", "txt"
 "text/plain", "text"
"text/xml", "xml"
"text/x-c++src", "c++"
"text/x-c++src", "cpp"
"text/x-component", "htc"
"text/x-csh", "csh"
"text/x-csrc", "c"
"text/x-dsrc", "d"
"text/x-haskell", "hs"
"text/x-java", "java"
"text/x-literate-haskell", "lhs"
"text/x-moc", "moc"
"text/x-pascal", "p"
"text/x-pascal", "pas"
"text/x-pcs-gcd", "gcd"
"text/x-setext", "etx"
"text/x-tcl", "tcl"
"text/x-tex", "tex"
"text/x-tex", "ltx"
"text/x-tex", "sty"
"text/x-tex", "cls"
"text/x-vcalendar", "vcs");
"text/x-vcard", "vcf");
"video/3gpp", "3gpp"
"video/3gpp", "3gp"
"video/3gpp", "3g2"
"video/dl", "dl"
"video/dv", "dif"
"video/dv", "dv"
"video/fli", "fli"
"video/m4v", "m4v"
"video/mpeg", "mpeg"
"video/mpeg", "mpg"
"video/mpeg", "mpe"
"video/mp4", "mp4"
"video/mpeg", "vob"
"video/quicktime", "qt"
"video/quicktime", "mov"
"video/vnd.mpegurl", "mxu"
"video/x-la-asf", "lsf"
"video/x-la-asf", "lsx"
"video/x-mng", "mng"
"video/x-ms-asf", "asf"
"video/x-ms-asf", "asx"
"video/x-ms-wm", "wm"
"video/x-ms-wmv", "wmv"
"video/x-ms-wmx", "wmx"
"video/x-ms-wvx", "wvx"
"video/x-msvideo", "avi"
"video/x-sgi-movie", "movie"
"x-conference/x-cooltalk", "ice"
"x-epoc/x-sisx-app", "sisx"
            
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论
1 楼 liuy2012 2012-06-29  
很强大 学习了~~

相关推荐

    open_file:一个可以调用原生APP打开带字符串文件导致flutter的插件,支持iOS(UTI) android(intent) PC(ffi) web(dart)

    打开文件 一个可以调用原生APP打开带字符串文件导致flutter的插件,支持iOS(DocumentInteraction) / android(intent) / PC(ffi) / web(dart...//OpenFile.open("/sdcard/example.txt", type: "text/plain", uti: "publ

    打开附件.java

    public static void openFile(String fileName){ if(fileName!=null && !"".equals(fileName)){ String name = fileName.toLowerCase(); if(name.lastIndexOf("doc") &gt;=0 || name.lastIndexOf("docx") &gt;=0){ //...

    Android判断和读取图标列表iconFile实例

    判断图标被点击的动作,操作触屏手机的基本要点,使用自定义的MyAdapter来将数据传入ListActivity,设置ListItem被点击时要做的动作,如果是文件夹就再运行getFileDir() ,如果是文件就运行openFile() ,调用...

    Android中调用系统所装的软件打开文件

    private void openFile&#40;File file&#41;{ Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //设置intent的Action属性 intent.setAction(Intent.ACTION_VIEW); //获取...

    android下文件选择

    toast(getText(R.string.open_file_error_format)); } } }; public boolean onKeyDown(int keyCode , KeyEvent event){ if(event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == ...

    Android in Action

    3.4 Understanding the AndroidManifest file 3.5 Summary 4 Intents and services 4.1 Working with Intent classes 4.2 Listening in with broadcast receivers 4.3 Building a Service 4.4 Performing Inter-...

    Professional Android 4 Application Development 源代码

    Android: An Open Platform for Mobile Development Native Android Applications Android SDK Features Introducing the Open Handset Alliance What Does Android Run On? Why Develop for Mobile? Why Develop ...

    新版Android开发教程.rar

    https://sites.google.com/a/android.com/opensource/release-features Android Android Android Android 盈利模式 Android 的 App Market 模式,软件开发者获得 7 成收入, 3 成用于系统维护。难点在于位置营销。 ...

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Understanding Android File Permissions 303 Working with Files and Directories 303 Exploring with the Android Application Directories 304 Working with Other Directories and Files on the Android File ...

    开发多媒体播放器

    openFile(pathEditText.getText().toString()); } else { //如果是目录打开目录下文件 getFileDir(pathEditText.getText().toString()); } } else { Toast.makeText...

    疯狂Android讲义源码

     8.2.1 openFileOutput和open  FileInput 312  8.2.2 读写SD卡上的文件 314  8.3 SQLite数据库 321  8.3.1 简介SQLiteDatabase 321  8.3.2 创建数据库和表 323  8.3.3 使用SQL语句操作SQLite  数据库 323  ...

    Google Android SDK开发范例大全(完整版)

    为了在 Eclipse 中创建应用程序,选择 File &gt; New &gt; Android project,这将启动 New Android Project 向导。 图 5. New Android project 向导 接下来,创建一个简单的应用程序,该应用程序有一个活动,并且在 ...

    Android二维码扫描

    intent.setAction("android.intent.action.VIEW"); Uri content_url = Uri.parse(obj.getText()); intent.setData(content_url); startActivity(intent); finish(); } }); dialog....

    CommonsWare.The.Busy.Coders.Guide.to.Android.Development.Version.8.2.2017

    Android, the next-generation open mobile platform from Google and the Open Handset Alliance, is poised to become a significant player in the mobile device market. The Android platform gives developers...

    android打开rar压缩文件

    代码如下:private void open(String fileString) { Intent intent = new Intent(); intent.setClassName(“com.rarlab.rar”, “com.rarlab.rar.MainActivity”); File file = new File&#40;fileString&#41;; ...

    疯狂Android讲义.part2

    8.2.1 openFileOutput和open FileInput 312 8.2.2 读写SD卡上的文件 314 8.3 SQLite数据库 321 8.3.1 简介SQLiteDatabase 321 8.3.2 创建数据库和表 323 8.3.3 使用SQL语句操作SQLite 数据库 323 8.3.4 使用sqlite3...

    疯狂Android讲义.part1

    8.2.1 openFileOutput和open FileInput 312 8.2.2 读写SD卡上的文件 314 8.3 SQLite数据库 321 8.3.1 简介SQLiteDatabase 321 8.3.2 创建数据库和表 323 8.3.3 使用SQL语句操作SQLite 数据库 323 8.3.4 使用sqlite3...

    Android代码-SVG浏览器

    Adds an intent filter so you can open SVG files from a file manager application such as CMFileManager. This application uses the official W3C SVG logo. If you don't want to build it yourself, you can ...

    android-a programmer's guide

    Open Handset Alliance and Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Introduction to Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

Global site tag (gtag.js) - Google Analytics