在Android中使用默认应用程序打开文件 [英] Open file with default applications in android

查看:97
本文介绍了在Android中使用默认应用程序打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用android做一个文件浏览器.因此,我想单击目录以外的任何文件时,都希望得到可以打开该文件的应用程序的建议,如果没有应用程序,则显示一个目录.我尝试了一些解决方案,但没有任何效果,所以,现在,我只是显示文件不是Toast中的目录

I am making a file explorer in android. So I want when any file is clicked other than directory I want to get a suggestion of apps which can open it and if no app is there then show a dailog. I tried some solutions but nothing worked, so, for now, I am just showing the file is not a directory in the toast

这是代码的一部分:

protected void onListItemClick(ListView l, View v, int position, long id) {
    String filename = (String) getListAdapter().getItem(position);
    if (path.endsWith(File.separator)) {
        filename = path + filename;
    } else {
        filename = path + File.separator + filename;
    }
    if (new File(filename).isDirectory()) {
        Intent intent = new Intent(this, ListFileActivity.class);
        intent.putExtra("path", filename);
        startActivity(intent);
    } else {
        Toast.makeText(this, filename + " is not a directory", Toast.LENGTH_LONG).show();
    }
}

推荐答案

Android有一些内置的

Android has some inbuilt Intent Action Type which helps you to Open or View specific files, but for this you need to know which type of file you are going to handle.

假设您可以使用按文档类型分类的文件类型,

Suppose If you have file type which categorized in document type you can use,

ACTION_OPEN_DOCUMENT 具有特定的 MIME_TYPE (Android 4.4或更高版本)

ACTION_OPEN_DOCUMENT with specific MIME_TYPE (Android 4.4 or Higher)

或者如果您要处理某些媒体文件(音频/视频)

or If you going to handle some media file (Audio/Video)

您可以使用

ACTION_VIEW

要识别特定文件的 MIME_TYPE ,您可以使用功能

To identify MIME_TYPE of specific file you can use function

guessContentTypeFromName(字符串url) 链接

getMimeTypeFromExtension(字符串扩展名) 链接

这篇关于在Android中使用默认应用程序打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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