【问题标题】:Open Folder - Intent chooser打开文件夹 - 意图选择器
【发布时间】:2012-02-02 04:04:32
【问题描述】:

如何打开一个对话框,列出所有可以打开给定文件夹的应用程序?

我尝试了以下代码

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
startActivity(Intent.createChooser(intent, "Open Folder"));

它说“没有应用程序可以执行此操作。”

我的设备有默认的文件资源管理器和 AndroidZip 应用程序(这些应用程序可以打开一个文件夹)。

【问题讨论】:

    标签: android


    【解决方案1】:

    它说“没有应用程序可以执行此操作。”

    这并不奇怪。您不是要求打开“文件夹”,因为Intent 系统中没有“文件夹”之类的东西。您正在尝试找出哪些应用程序可以打开没有文件扩展名和 MIME 类型的路径。在您的设备上安装的任何应用程序都没有在没有文件扩展名和 MIME 类型的路径上支持 ACTION_GET_CONTENT 的活动上具有 <intent-filter>

    你可以试试ACTION_VIEW。但是,请记住,我估计 90% 以上的 Android 设备也不会以这种方式处理“文件夹”。

    【讨论】:

    • 谢谢你,CW!你真的是一个专业人士,对别人比专业人士更友善。
    • 一个简单的问题,如何导航到文件夹?假设该文件夹包含所有 mp3。
    • @Aashish:如果您阅读我的回答,Intent 系统中没有“文件夹”之类的东西。
    • 好吧,我的场景是当我点击一个按钮时,它应该导航到给定的路径。这不可能?
    • @Aashish:如果您阅读了我的回答,欢迎您尝试ACTION_VIEW,但除非用户碰巧安装了某种文件管理器应用程序,否则它不太可能工作。
    【解决方案2】:

    在这里试试这个。对我有用。

    public void openFolder()
    {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
        + "/yourFolder/");
    intent.setDataAndType(uri, "*/*");
    startActivity(Intent.createChooser(intent, "Open folder"));
    }
    

    另见here

    【讨论】:

      【解决方案3】:

      因为目录文件似乎没有标准的 mime 类型,所以我编写了文件资源管理器来过滤没有 mime 类型的意图。只需将数据设置为文件夹的 Uri。

      Intent intent = new Intent(Intent.ACTION_VIEW);
      Uri uri = Uri.parse("file:///sdcard/MyFolder");
      intent.setData(uri);
      startActivity(intent);
      

      此意图将启动以下应用:

      http://play.google.com/store/apps/details?id=com.organicsystemsllc.thumbdrive

      我发现这也适用于根据扩展名打开文件或文件夹。在下面的代码 sn-p 中,文件夹 uri 的文件扩展名和 mime 类型最终为 null。这仍然适用于匹配过滤方案“文件”但没有特定 mime 类型的活动,即拇指驱动器。请注意,当 uri 是目录时,此方法不会找到过滤所有文件 mime 类型的活动。

      //Get file extension and mime type
      Uri selectedUri = Uri.fromFile(file.getAbsoluteFile());
      String fileExtension =  MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
      String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
      
      //Start Activity to view the selected file
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setDataAndType(selectedUri, mimeType);
      startActivity(Intent.createChooser(intent, "Open File..."));
      

      【讨论】:

        【解决方案4】:

        有效:

        Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/myFolder/");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(selectedUri, "resource/folder");
        startActivity(intent);
        

        编码愉快:)

        【讨论】:

        • 代码对我不起作用。它不会打开文件夹,而是抛出未找到活动的异常。
        • 很可能您的设备上没有安装资源管理器应用程序。请检查一下。
        • @Sa Qada 我们可以使用默认文件管理器打开吗?默认文件管理器是否具有意图过滤器 ACTION_VIEW ?
        【解决方案5】:

        不幸的是,就像@CommonsWare 所说,在现有的 Android 系统中不存在“文件资源管理器”的标准定义(除非你安装了第 3 方“文件资源管理器”)..
        这就是为什么 "resource/folder" 默认情况下 mime-type 不起作用..

        我找到的最大值是:https://stackoverflow.com/a/41614535/2377343

        【讨论】:

          【解决方案6】:

          我搜索了很多,找到了内容类型vnd.android.document/directory。 此外,这包括针对目标 API 24 及更高版本的其他解决方案。它显示了打开它的两个应用程序:OpenIntent 文件管理器和内置文件管理器。

          这是研究。而不是一个完整的答案。如果您发现内置文件管理器的参数应该是什么,请在此处告诉我们。

          File saveLocations 即指向 DCIM 文件夹。

              // open a location with the file exporer
              // see https://stackoverflow.com/a/26651827/1320237
              // see https://stackoverflow.com/a/38858040
              // see https://stackoverflow.com/a/8727354
              Uri saveLocationUri =  FileProvider.getUriForFile(this, this.getPackageName() + ".provider", saveLocation);
              final Intent openSaveLocationIntent = new Intent(Intent.ACTION_VIEW);
              openSaveLocationIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
              openSaveLocationIntent.setDataAndType(saveLocationUri, "vnd.android.document/directory");
              // see http://www.openintents.org/action/android-intent-action-view/file-directory
              openSaveLocationIntent.putExtra("org.openintents.extra.ABSOLUTE_PATH", saveLocation.toString());
              if (openSaveLocationIntent.resolveActivityInfo(getPackageManager(), 0) != null) {
          
                          startActivity(openSaveLocationIntent);
              } else  {
                  // if you reach this place, it means there is no any file
                  // explorer app installed on your device
          
              }
          

          看来,Android 24+ 有可能。

          有一个意图过滤器来打开目录:

              <activity
                  android:name=".files.FilesActivity"
                  android:documentLaunchMode="intoExisting"
                  android:theme="@style/DocumentsTheme">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
                  </intent-filter>
                  <intent-filter>
                      <action android:name="android.intent.action.VIEW" />
                      <category android:name="android.intent.category.DEFAULT" />
                      <data android:mimeType="vnd.android.document/root" />
                  </intent-filter>
                  <intent-filter>
                      <action android:name="android.intent.action.VIEW" />
                      <category android:name="android.intent.category.DEFAULT" />
                      <data android:mimeType="vnd.android.document/directory" />
                  </intent-filter>
              </activity>
          

          这里有更多代码: https://github.com/derp-caf/packages_apps_DocumentsUI/blob/ab8e8638c87cd5f7fe1005800520e739b3a48cd5/src/com/android/documentsui/ScopedAccessActivity.java#L114

          我不确定如何正确设置参数。

                          // Sets args that will be retrieve on onCreate()
                          final Bundle args = new Bundle();
                          args.putString(EXTRA_FILE, file.getAbsolutePath());
                          args.putString(EXTRA_VOLUME_LABEL, volumeLabel);
                          args.putString(EXTRA_VOLUME_UUID, isPrimary ? null : storageVolume.getUuid());
                          args.putString(EXTRA_APP_LABEL, appLabel);
                          args.putBoolean(EXTRA_IS_ROOT, isRoot);
                          args.putBoolean(EXTRA_IS_PRIMARY, isPrimary);
          

          我尝试输入一些额外信息,但它仍会在根文件夹中打开活动。

              // from https://github.com/derp-caf/packages_apps_DocumentsUI/blob/ab8e8638c87cd5f7fe1005800520e739b3a48cd5/src/com/android/documentsui/ScopedAccessActivity.java#L114
              openSaveLocationIntent.putExtra("com.android.documentsui.FILE", saveLocation.toString());
              openSaveLocationIntent.putExtra("com.android.documentsui.IS_ROOT", false); // is the root folder?
              openSaveLocationIntent.putExtra("com.android.documentsui.IS_PRIMARY", true); // is the primary volume? SD-Card should be false
              openSaveLocationIntent.putExtra("com.android.documentsui.APP_LABEL", getTitle());
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-31
            • 1970-01-01
            • 2010-09-24
            • 1970-01-01
            • 1970-01-01
            • 2018-01-12
            相关资源
            最近更新 更多