android - 使用默认资源管理器查看 Android 中的文件夹内容

标签 android android-intent

我想以编程方式启动默认文件资源管理器来显示文件夹的内容。

我正在使用这段代码,但它崩溃了:

    Uri startDir = Uri.fromFile(new File("/sdcard/DCIM/Camera")); 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_VIEW); 
    intent.setData(startDir);
    startActivity(intent);

LogCat 显示“找不到处理 Intent 的 Activity ”...

执行此操作的最佳选择是什么?我希望用户能够看到文件夹的内容并能够单击文件(例如,单击视频并使用默认播放器启动它,单击 PDF 并打开它等)。

最佳答案

不幸的是,似乎没有标准的方法可以做到这一点,我之前一直在寻找完全相同的东西,但找不到任何解决方案。有 2 种替代方法可能适合您:

1) 使用通用 Intent 并让用户选择他/她的文件管理器 这是安全和容易的,但离我们真正想要的有点远

Uri startDir = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
        .getAbsolutePath() + "/DCIM/Camera"));
    Intent intent = new Intent();
    intent.setData(startDir);
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_VIEW);
    startActivity(intent);

不要使用intent.setType("file/*");,它不是标准的MIME类型。

2) 使用著名文件管理器提供的特定 Intent 众所周知的文件管理器有自己的自定义 Intent 过滤器,可以接受允许简单浏览的目录路径。其中一些在这里:OI file manager , ES Explorer

也许您可以检查用户是否安装了这些特定的文件管理器,然后使用此解决方案,否则回退到一般 Intent 。

目前您只有这两个选项。如果我发现任何更好的解决方案,我会更新这篇文章。

关于android - 使用默认资源管理器查看 Android 中的文件夹内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17843962/

相关文章:

java - 在 Java/Android 中,为什么我能够从 ParcelFileDescriptor 获取整数文件描述符?

android - 在 Activity 中嵌入 Google 日历

java - 是否可以确定从 Activity 发回的数据是 String 还是 String[]?

java - HashMap 传递给 Activity

Android - 通过 put/getExtra 启动带有额外信息的服务

android react-native在谷歌审批过程中避免 "Background location access not declared"

android - 有什么方法可以知道哪个xml文件没有在我们的项目中使用

android - Android 中使用视频 View 的视频横向支持

android - 适用于 Android 和 iOS 的跨平台开发

android - 启动新 Activity 时发生 TransactionTooLargeException