代码之家  ›  专栏  ›  技术社区  ›  Jack

Android-当使用Intent.ACTION\u GET\u内容调用“文件选择器”时,如何仅显示(或能够选择)具有自定义扩展名的文件

  •  5
  • Jack  · 技术社区  · 7 年前

    我知道您可以限制在名为的文件资源管理器中显示的可用文件类型 Intent.ACTION_GET_CONTENT setType() ,但这只能用于已知的文件扩展名,如.jpg、.pdf、.docx,我需要的是只显示具有 ,例如.abc,因此用户只能选择以.abc结尾的文件。我已经搜索了很长时间,仍然找不到有效的方法来解决这个问题;最接近的方法是创建自定义mime类型,如下所示:

                 <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:scheme="file" />
                    <data android:mimeType="application/customtype" />
                    <data android:pathPattern=".*\\.abc" />
                    <data android:host="*" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:scheme="content" />
                    <data android:mimeType="application/customtype" />
                    <data android:pathPattern=".*\\.abc" />
                    <data android:host="*" />
                </intent-filter>
    

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/customtype"); 
    startActivityForResult(intent,FILE_SELECTOR_REQUEST_CODE);
    

    1 回复  |  直到 7 年前
        1
  •  3
  •   IgorGanapolsky    7 年前

    数据安卓:mimeType=“应用程序/自定义类型”