从通知中打开文件 [英] Open file from notification

查看:69
本文介绍了从通知中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器下载文件.对于此操作,我正在显示进度不确定的通知.下载文件后,我想通过单击通知打开它.

I download a file from a server. For this action I'm showing a notification with indeterminate progress. When the file will be downloaded I want to open it with a click on the notification.

我得到了扩展名,并尝试使用intent来打开它

I get the extension and try to open it with intent like this

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);
}

我的问题是,对于任何扩展名,选择器上的消息为没有应用程序可以执行此操作.我想念什么吗?

My problem is that with any extension the message on the chooser is No app can perform this action. Am I missing something?

推荐答案

好,我找到了.我替换

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

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

它奏效了

这篇关于从通知中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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