2017-02-18 41 views
-1

我想为android做我自己的文件管理器。 我想知道如何让用户选择他想要使用的应用程序打开文件。比如当你打开一个.doc文件时,你有一堆.doc文件阅读器。我如何能够检索用户可以用来读取/打开文件的应用程序列表?如何让用户选择他想用来打开文件的应用程序?

Here's a screenshot.

回答

0

试试这个代码

Uri uri = Uri.parse("Your file name: //"+file.getAbsolutePath()); 
Intent intent = new Intent(); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setAction(Intent.ACTION_VIEW); 
String type = "application/msword"; 
intent.setDataAndType(Uri.fromFile(file), type); 
startActivity(intent); 
0

您应该使用隐含的意图你的目的。以下是播放视频文件的选项示例代码

Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*"); 
    startActivity(Intent.createChooser(intent, "Complete action using")); 

我希望它能帮助你。欲了解更多详情可参考Android的官方文件,如下链接

https://developer.android.com/training/basics/intents/sending.html#AppChooser

+0

当我点击的PNG文件。它不会显示应用列表中的图库。如果文件类型是png,我该如何制作它,应用程序列表是用于打开图像的。当它是.doc它显示文档阅读器?抱歉。我真的很难编程。编辑:哦,我认为这是setDataAndType代码。大声笑。我想试试 – Kon

+0

嗯试试,让我知道如果你发现任何问题 –

+0

我用这段代码'Intent intent = new Intent(Intent.ACTION_VIEW); String title = getResources()。getString(R.string.chooser_title); intent.setType(“*/*”); Intent chooser = Intent.createChooser(intent,title);' 它显示了一堆不同的应用程序。这就是我所能做的。我不知道如何让它仅显示用于.doc和.pdf文件的图像和文件读取器的图库应用程序。我很愚蠢哈哈 – Kon

0

提到的我觉得你可以试试这个

Intent in= new Intent(Intent.ACTION_MAIN); 
in.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity")); 
startActivity(in); 
相关问题