android - 自动打开 Android 文件的 Intent Chooser

标签 android file android-intent mime-types

我想在 android 中打开一个文件。我想要做的是,如果文件是 Image 类型,那么我想打开 Intent Chooser,其中包含可以查看图像的应用程序,如果它是视频类型,然后使用可以查看视频的应用程序打开 Intent Chooser。我怎样才能做到这一点?

最佳答案

我找到了解决办法。我将它粘贴在这里,以便它可以帮助其他用户。

    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File(path);

    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
    String type = mime.getMimeTypeFromExtension(ext);

    intent.setDataAndType(Uri.fromFile(file), type);

    context.startActivity(intent);

关于android - 自动打开 Android 文件的 Intent Chooser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148966/

相关文章:

android - Gradle:如何为类(class)运行仪器测试

android - 使用单个 Fragment 执行多个 UI 任务?

Android不将资源打包到apk中

java - 使用 Apache Commons VFS2 检查文件是否存在

android - 在 AndroidManifest.xml 中哪里添加 Intent 过滤器?

android - 从处理程序启动新 Activity

Android - 支持库 v24.0.0 中的矢量绘图

c - 尝试写入日志文件时权限被拒绝

python - 使用通配符搜索文件

android - 获取通知 Intent 附加功能