【问题标题】:Android: How to open a specific folder via Intent and show its content in a file browser?Android:如何通过 Intent 打开特定文件夹并在文件浏览器中显示其内容?
【发布时间】:2013-06-14 11:54:25
【问题描述】:

我以为这很容易,但不幸的是事实并非如此。

我有什么:

我的外部存储设备上有一个名为“myFolder”的文件夹(不是 sd 卡,因为它是 Nexus 4,但这应该不是问题)。该文件夹包含一些*.csv 文件。

我想要什么:

我想编写一个方法,它执行以下操作:显示各种应用程序(文件浏览器),我可以从中选择一个(见图)。单击它后,选定的文件浏览器应该启动并显示“myFolder”的内容。不多不少。

我的问题:

我该怎么做?我想我非常接近以下代码,但无论我做什么 - 而且我确信一定有一些我还没有做对的东西 - 它总是只打开外部存储中的主文件夹。

public void openFolder()
{
File file = new File(Environment.getExternalStorageDirectory(),
    "myFolder");

Log.d("path", file.toString());

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivity(intent);
}

【问题讨论】:

标签: android android-intent directory android-sdcard


【解决方案1】:

你看起来很亲密。

我会尝试这样设置 URI:

String folderPath = Environment.getExternalStorageDirectory()+"/pathTo/folder";

Intent intent = new Intent();  
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri myUri = Uri.parse(folderPath);
intent.setDataAndType(myUri , "file/*");   
startActivity(intent);

但这与您尝试过的并没有太大不同。 告诉我们它是否改变了什么?

还要确保目标文件夹存在,并在将其发送到 intent 之前查看生成的 Uri 对象,这可能不是我们所期望的.

【讨论】:

  • 不,不幸的是,这 工作。结果与我上面的代码相同。
  • 'myUri' 对象在发送到意图之前有什么内容?
  • “URI 中有什么”是什么意思?你是说uri.toString()之类的吗?
  • 嗨!请看类似的问题stackoverflow.com/questions/50072638/…
【解决方案2】:

我终于让它工作了。这样,选择器仅显示少数应用程序(Google Drive、Dropbox、Root Explorer 和 Solid Explorer)。两个浏览器都可以正常工作,但 Google Drive 和 Dropbox 不行(我猜是因为他们无法访问外部存储)。 "*/*" 等其他 MIME 类型也是可以的。

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

【讨论】:

  • OI 文件管理器等一些应用可以使用此 Intent,但 Android 的默认文件管理器无法使用此方法。
  • 这对我来说只是为 DropBox 工作。您能否提供修改后的代码以在 SD 卡中打开文件夹。
  • @SureshSharma 您想使用哪个文件浏览器打开 SD 卡上的文件夹?
  • 这工作正常,但在三星手机中它没有重定向到特定文件夹
  • getExternalStorageDirectory() 已被弃用。有什么新的解决方案吗?
【解决方案3】:

此代码将与 OI 文件管理器一起使用:

        File root = new File(Environment.getExternalStorageDirectory().getPath()
+ "/myFolder/");
        Uri uri = Uri.fromFile(root);

        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setData(uri);
        startActivityForResult(intent, 1);

您可以在此处获取 OI 文件管理器:http://www.openintents.org/en/filemanager

【讨论】:

  • 我不得不用 Uri.parse(...) 替换 Uri.fromFile(...) - 如 OP 的问题和接受的答案所示。
【解决方案4】:

这应该可行:

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

if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
{
    startActivity(intent);
}
else
{
    // if you reach this place, it means there is no any file 
    // explorer app installed on your device
}

请确保您的设备上安装了任何文件浏览器应用程序。

编辑:在评论中添加了 shantanu 的建议。

图书馆: 如果当前的解决方案对您没有帮助,您还可以查看以下库 https://android-arsenal.com/tag/35

【讨论】:

  • 使用此代码以避免在未安装应用程序时崩溃。意图选择器 = Intent.createChooser(intent, title); // 验证 Intent 将解析到至少一个活动 if (intent.resolveActivity(getPackageManager()) != null) { startActivity(chooser); }
  • @shantanu,谢谢你的推荐,我添加了一个类似的方法来检查意图的存在。
  • 这必须被标记为答案...诀窍是安装文件资源管理器.. Es文件资源管理器对我有用,但对OI文件管理器不起作用......
  • 嘿伙计,这只适用于安装了 ES 文件资源管理器的设备,因为 mime 'resource/folder' 不是标准的 mime 类型。
  • 这确实似乎只适用于 ES 文件资源管理器。如果有适用于其他资源管理器应用的解决方案,请告诉我。
【解决方案5】:
File temp = File.createTempFile("preview", ".png" );
String fullfileName= temp.getAbsolutePath();
final String fileName = Uri.parse(fullfileName)
                    .getLastPathSegment();
final String filePath = fullfileName.
                     substring(0,fullfileName.lastIndexOf(File.separator));
Log.d("filePath", "filePath: " + filePath);

完整文件名

/mnt/sdcard/Download_Manager_Farsi/preview.png

文件路径

/mnt/sdcard/Download_Manager_Farsi

【讨论】:

    【解决方案6】:
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    
    intent.setType("text/csv");
    
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    
    try {
          startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), 0);
    
    } catch (android.content.ActivityNotFoundException ex) {
      ex.printStackTrace();
    }
    

    那么你只需要添加响应

    public void  onActivityResult(int requestCode, int resultCode, Intent data){
    
    switch (requestCode) {
      case 0: {
         //what you want to do
        //file = new File(uri.getPath());
      }
    }
    }
    

    【讨论】:

      【解决方案7】:
      Intent chooser = new Intent(Intent.ACTION_GET_CONTENT);
      Uri uri = Uri.parse(Environment.getDownloadCacheDirectory().getPath().toString());
      chooser.addCategory(Intent.CATEGORY_OPENABLE);
      chooser.setDataAndType(uri, "*/*");
      // startActivity(chooser);
      try {
      startActivityForResult(chooser, SELECT_FILE);
      }
      catch (android.content.ActivityNotFoundException ex)
      {
      Toast.makeText(this, "Please install a File Manager.",
      Toast.LENGTH_SHORT).show();
      }
      

      在上面的代码中,如果 setDataAndType 为“*/*”,则打开内置文件浏览器以选择任何文件,如果我设置“text/plain”,则打开 Dropbox。我安装了 Dropbox、Google Drive。如果我卸载 Dropbox,只有“*/*”可以打开文件浏览器。这是安卓 4.4.2。我可以通过 getContentResolver().openInputStream(data.getData()) 从 Dropbox 和 Google Drive 下载内容。

      【讨论】:

        【解决方案8】:

        今天,您应该使用其内容表示一个文件夹:从存储访问框架获得的 URI,打开它应该很简单:

        Intent i = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(i);
        

        【讨论】:

        • 如何指定uri?我试过val filePath = Uri.parse(Environment.DIRECTORY_DOCUMENTS + "/myFolder/") 不起作用。
        • @SamChen 从存储访问框架获得是这里的关键字。这不适用于文件:URI。
        【解决方案9】:
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, DocumentsContract.Document.MIME_TYPE_DIR);
        startActivity(intent);
        

        将打开文件应用主屏幕

        【讨论】:

        • 这甚至不会从通知中打开应用程序选择器。它只是没有做任何事情。
        【解决方案10】:

        线程很旧,但我需要在我的应用程序中使用这种功能,并且我找到了一种方法,所以我决定发布它,如果它可以帮助我遇到这种情况的任何人。

        由于我们的设备群仅由三星 Galaxy Tab 2 组成,我只需要找到文件资源管理器的包名称,提供正确的路径,然后我就可以成功打开我想要的文件资源管理器。我希望我可以使用Intent.CATEGORY_APP_FILES,但它仅在 API 29 中可用。

          Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.sec.android.app.myfiles");
          Uri uri = Uri.parse(rootPath);
          if (intent != null) {
              intent.setData(uri);
              startActivity(intent);
          }
        

        正如我所说,这对我来说很容易,因为我们的客户拥有相同的设备,但它可以帮助其他人找到适合自己情况的解决方法。

        【讨论】:

          【解决方案11】:

          这是我的答案

          private fun openFolder() {
              val location = "/storage/emulated/0/Download/";
              val intent = Intent(Intent.ACTION_VIEW)
              val myDir: Uri = FileProvider.getUriForFile(context, context.applicationContext.packageName + ".provider", File(location))
              intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
              intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
                  intent.setDataAndType(myDir,  DocumentsContract.Document.MIME_TYPE_DIR)
              else  intent.setDataAndType(myDir,  "*/*")
          
              if (intent.resolveActivityInfo(context.packageManager, 0) != null)
              {
                  context.startActivity(intent)
              }
              else
              {
                  // if you reach this place, it means there is no any file
                  // explorer app installed on your device
                  CustomToast.toastIt(context,context.getString(R.string.there_is_no_file_explorer_app_present_text))
              }
          }
          

          这就是我使用 FileProvider 的原因 - android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

          我在这台设备上测试过
          设备:三星 SM-G950F (dreamltexx),操作系统 API 级别:28

          【讨论】:

          • 我需要在清单中设置文件提供程序吗?
          • 不知道你是怎么做到的,getUriForFiles 在传递文件夹时抛出越界异常,如那里所述:stackoverflow.com/questions/54385933/…
          • 您可能认为它有效,因为您指定了下载文件夹。问题是指定其他文件夹时它不起作用。
          猜你喜欢
          • 2017-07-27
          • 1970-01-01
          • 1970-01-01
          • 2012-09-17
          • 2018-06-10
          • 1970-01-01
          • 2018-01-02
          • 1970-01-01
          • 2013-01-14
          相关资源
          最近更新 更多