try {            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);            intent.addCategory(Intent.CATEGORY_OPENABLE);            intent.setType("*/*");            activity.startActivityForResult(intent, 1001);        } catch (ActivityNotFoundException e) {            Log.e(TAG,e.getMessage());        }

这是正常的访问系统自带的文件管理器。但是setType只支持单个setType一般是以下这种(以只查看图片文件为例):

intent.setType("image/*");

然后限制多种类型,百度上一大堆的错误示范(如下):

intent.setType("image/*;audio/*");

 这些是根本无法做到限制的。

正确的做法(比如我要限制只查看ppt ,doc,docx,pptx,pdf等文件)如下:

public static final String DOC = "application/msword";    public static final String DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";    public static final String XLS = "application/vnd.ms-excel application/x-excel";    public static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";    public static final String PPT = "application/vnd.ms-powerpoint";    public static final String PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";    public static final String PDF = "application/pdf";  try {                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);                    intent.addCategory(Intent.CATEGORY_OPENABLE);                    //设置doc,docx,ppt,pptx,pdf 5种类型                    intent.setType("application/msword|application/vnd.openxmlformats-officedocument.wordprocessingml.document" +                            "|application/vnd.ms-powerpoint|application/vnd.openxmlformats-officedocument.presentationml.presentation|application/pdf");                    //在API>=19之后设置多个类型采用以下方式,setType不再支持多个类型                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {                        intent.putExtra(Intent.EXTRA_MIME_TYPES,                                new String[]{DOC,DOCX, PPT, PPTX,PDF});                    }                    startActivityForResult(intent, 1001);                } catch (ActivityNotFoundException e) {                }

 android intent.setType指定浏览本地多种类型的文件_第1张图片

然后在跳转到手机自带的文件管理文件后,只有限制的几种类型才是能进行点击/选择。

更多相关文章

  1. 修改文件夹权限
  2. Android——文件操作
  3. Android下使用Properties文件保存程序设置
  4. android解析xml文件 Android DOM解析XML之全球实时地震信息列表
  5. android 删除文件,打开指定的文件类型
  6. Android Studio2.0引入so文件(亲测)
  7. 引用自定义资源需注意数据类型
  8. eclipse创建android项目,无法正常预览布局文件
  9. Android lint 删除无用图片文件和配置文件

随机推荐

  1. Android培训---创建Android工程
  2. Android(安卓)ApiDemos示例解析(53):Grap
  3. 【OpenCV】编译opencv+opencv_contrib sd
  4. android 标题栏的高度
  5. Xamarin.Forms Android的本地数据库SQLit
  6. 动态调试Android笔记
  7. 最小物联网系统设计——使用说明
  8. android仿QQ列表实现
  9. 自定义View实现HTML图文环绕效果
  10. Android(安卓)之 Activity (一) 基础知识