android上获得打开相应文件类型的intent

来源:互联网 发布:淘宝花呗分期付款 编辑:程序博客网 时间:2024/06/14 13:25

在开发安卓app时,我们时常会需要打开指定文件类型的文件,这时我们可以通过获得一个相应的intent来调用手机已有的app来将文件打开。当然,你也可以开发自己的文件浏览器。

打开文件: apk,text,html,video,audio,image和其他

//获得打开apk文件的intent   public static Intent getApkFileIntent(String filePath)throws Exception{       Intent intent = new Intent();       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    intent.setAction(android.content.Intent.ACTION_VIEW);       Uri uri = Uri.fromFile(new File(filePath));       intent.setDataAndType(uri,"application/vnd.android.package-archive");       return intent;   }   //获得打开Text文件的intent   public static Intent getTextFileIntent(String filePath,boolean paramBoolean)throws Exception{       Intent intent = new Intent("android.intent.action.VIEW");       intent.addCategory("android.intent.category.DEFAULT");       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       Uri uri = null;       if(paramBoolean){           uri = Uri.parse(filePath);       }else{           uri = Uri.fromFile(new File(filePath));       }       intent.setDataAndType(uri, "text/plain");       return intent;   }   //获得打开Html文件的intent   public static Intent getHtmlFileIntent(String filePath,String contentType)throws Exception{       Intent intent=new Intent();       intent.setAction("android.intent.action.VIEW");       //content可能是较低版本的android才可以打开的     //Uri uri = Uri.parse("content://com.android.htmlfileprovider" + filePath);       Uri uri = Uri.parse("file://" + filePath);       intent.setData(uri);       intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");       return intent;   }   public static Intent startBrowser(String filePath)throws Exception{       Uri uri = Uri.parse(filePath);       Intent intent = new Intent();       intent.setAction("android.intent.action.VIEW");       intent.addCategory("android.intent.category.BROWSABLE");       intent.setData(uri);       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       boolean explicitMode=false;       String scheme=uri.getScheme();       if(scheme!=null&&scheme.startsWith("file")) {           explicitMode=true;       }       if(explicitMode) {           intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");       } else {           intent.addCategory("android.intent.category.BROWSABLE");       }       return intent;   }   //获得打开video文件的intent   public static Intent getVideoFileIntent(String filePath,String contentType)throws Exception{       Intent intent =  new Intent("android.intent.action.VIEW");       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);       intent.putExtra("oneshot",0);       intent.putExtra("configchange", 0);       Uri uri = Uri.fromFile(new File(filePath));       intent.setDataAndType(uri,contentType);       return intent;   }   //获得打开audio文件的intent。和video只有content-type不同   public static Intent getAudioFileIntent(String filePath,String contentType)throws Exception{       Intent intent = new Intent("android.intent.action.VIEW");       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);       intent.putExtra("oneshot",0);       intent.putExtra("configchange",0);       Uri uri = Uri.fromFile(new File(filePath));       intent.setDataAndType(uri,contentType);       return intent;   }   //获得打开image文件的intent。和video只有content-type不同   public static Intent getImageFileIntent(String filePath,String contentType)throws Exception{       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(filePath));       intent.setDataAndType(uri, contentType);       return intent;   }    //获得打开其他文件类型的intent    public static Intent getOtherFileIntent(String filePath,String contentType)throws Exception{        Intent intent = new Intent();        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        intent.setAction(android.content.Intent.ACTION_VIEW);        Uri uri = Uri.fromFile(new File(filePath));        intent.setDataAndType(uri,contentType);        return intent;    }

  这里特别需要注意的是获得打开的本地html文件的intent,以上代码有两个函数可以用于打开本地html文件的函数startBrowergetHtmlFileIntent。在我是实现中,startBrowser并不能成功调用到我手机的浏览器,getHtmlFileIntent可以调用到我手机的原生浏览器。至于startBrowser 为什么没有成功地调用到手机的浏览器,博主暂时没有找到原因,找到再做更新,嘻嘻。如有知道,也欢迎各位指点,我也将表示万分感激。

  此外,getHtmlFileIntent 函数的使用还要注意android版本的高低设置合适的url格式,是用 Uri uri = Uri.parse("file://" + filePath); 还是用 Uri.parse("content://com.android.htmlfileprovider" + filePath);。如果用你的手机判断,可以尝试打开手机本地的一个html,查看地址栏便可得知。

将以上函数打包成一个函数

    //获得打开文件的intent    public static Intent getIntentToOpenFile( String filePath,String contentType)throws Exception {        if(contentType == null || contentType.length() == 0){            return null;        }else if(contentType.equals("text/html")){            return getHtmlFileIntent(filePath,contentType);          //return startBrower(filePath);        }else if(contentType.equals("text/plain")){            return getTextFileIntent(filePath, false);        }else if(contentType.equals("application/vnd.android.package-archive")) {            return getApkFileIntent(filePath);        }else if(contentType.startsWith("image")) {            return getImageFileIntent(filePath,contentType);        }else if(contentType.startsWith("audio")) {            return getAudioFileIntent(filePath,contentType);        }else if(contentType.startsWith("video")) {            return getVideoFileIntent(filePath,contentType);        }        return getOtherFileIntent(filePath,contentType);    }

  最后我们只要在activity中调用startActivity(intent); 就可以了。

  最后,对于如何获得文件的content-type,可以看我的另一篇博文获取文件的拓展名和content-type – java实现

  以上代码如有错误或者不佳之处,欢迎指出,如有更好的方法,也欢迎一起讨论。

0 0
原创粉丝点击