android - 从通知打开文件

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

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

最佳答案

好的,我找到了。我替换

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

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

成功了

关于android - 从通知打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35762471/

相关文章:

android - 如何在 Android 的应用程序中显示文件预览和打开文件?

java - 适配器内 Intent 的 Android 错误消息

java - Android Studio 带有分数的测验应用程序

android - GLES 错误 : vertex attribute array is enabled with no data bound

css - Gwt:CellTable CSS 删除所有其他 CSS 文件

Python搜索文件并创建输出,写入内容

android - Cordova android 文件系统困难

android - 单击 ListView 项目时启动应用程序

android - adb 命令使用 workmanager 列出所有计划的工作

android - 使用游标从 SQLite 中扣除值