从通知中打开文件

时间:2016-03-03 03:11:45

标签: android file android-intent

我从服务器下载文件。对于此操作,我会显示一个包含不确定进度的通知。当我下载文件时,我想点击通知打开它。

我获得了扩展程序并尝试使用intent这样的

打开它
public static Intent openFile(Context context, File file) {
    String fileName = file.getName();

    String extension = fileName.substring(fileName.lastIndexOf(".") + 1);


    String type = "application/" + extension;

    if (getAttachmentType(fileName) == AttachmentType.IMAGE) {
        type = "image/*";
    } else if (getAttachmentType(fileName) == AttachmentType.AUDIO) {
        type = "audio/*";
    } else if (getAttachmentType(fileName) == AttachmentType.VIDEO) {
        type = "video/*";
    } else if (getAttachmentType(fileName) == AttachmentType.WORD) {
        type = "application/msword";
    } else if (getAttachmentType(fileName) == AttachmentType.EXCEL) {
        type = "application/vnd.ms-excel";
    } else if (getAttachmentType(fileName) == AttachmentType.POWERPOINT) {
        type = "application/vnd.ms-powerpoint";
    } else if (getAttachmentType(fileName) == AttachmentType.TXT) {
        type = "text/*";
    }

    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Intent chooser = Intent.createChooser(intent, context.getResources().getString(R.string.open_file_with));
    intent.setDataAndType(path, type);
    return chooser;
}

并在通知上执行此操作

Intent intent = ActionsHelper.openFile(context, file);
if (intent != null) {
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
      mBuilder.setContentIntent(pendingIntent);
}

我的问题是,对于任何扩展程序,选择器上的消息是没有应用程序可以执行此操作。我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

好的,我找到了。我替换

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

并且有效

答案 1 :(得分:0)

您正在设置Intent数据,并在创建选择器Intent后输入。切换到:

Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, type);

Intent chooser = Intent.createChooser(intent, context.getResources().getString(R.string.open_file_with));