自动开启意向选配该文​​件的Andr​​oid [英] Automatically open Intent Chooser for the file Android

查看:138
本文介绍了自动开启意向选配该文​​件的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开机器人的文件。我想要做的是,如果该文件类型的图片然后我想打开意图选配,其中包含应用程序可以查看该图像,而如果它是视频类型的,然后打开意图选择器与该可以查看视频的应用程序。我怎样才能做到这一点?

I want to open a file in android. What i want to do is if the file is of type Image then i want to open Intent Chooser which contains applications that can view the image, and if it is of video type, then open Intent Chooser with applications that can view videos. How can i achieve this?

推荐答案

我已经找到了解决办法。我将其粘贴在这里,因此它可能帮助其他用户。

I have found a solution. I am pasting it here so it may help other users.

    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().indexOf(".") + 1);
    String type = mime.getMimeTypeFromExtension(ext);

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

    context.startActivity(intent);

这篇关于自动开启意向选配该文​​件的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆