如何从图库中选择文件?

时间:2014-03-19 12:52:01

标签: android android-gallery

我想选择一个文件(图像,视频或音频除外),如pdf,ppt,docx,txt等。

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),Constant.SELECT_FILE);

以上方法无效。

当我执行操作时出现对话框,并显示消息“没有应用程序可以执行此操作”

2 个答案:

答案 0 :(得分:2)

因为设备上没有安装任何应用,而是通过操作Intent.ACTION_GET_CONTENT处理您的意图。因此,如果您的设备在Android 4.4上运行,请尝试使用Intent.ACTION_OPEN_DOCUMENT或实施您自己的文件浏览器活动。

查看https://github.com/vaal12/AndroidFileBrowseraFileChooser

答案 1 :(得分:0)

试试这个:

问题是没有安装应用程序来处理打开PDF。您应该使用Intent Chooser,如下所示:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Instruct the user to install a PDF reader here, or something
}