打开通知文件,单击使用Retrofit2下载

时间:2019-03-06 13:05:33

标签: android android-intent android-notifications android-pendingintent

我正在使用改造Web服务下载PDF。然后,我将显示一个自我创建的通知,以表明该文件已下载。但是我不明白单击通知时如何显示或打开文件。

这是我的通知电话:

RestClient.webServices()
        .downloadFile(id)
        .enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                if (response.isSuccessful()) {

                    boolean writtenToDisk = writeResponseBodyToDisk(response.body());

                    Log.e(TAG, "file download was a success? " + writtenToDisk);

                    if (writtenToDisk) {

                        showToast("Invoice downloaded successfully");
                        showDownloadNotification();

                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

这是writeResponseBodyToDisk()函数:

private boolean writeResponseBodyToDisk(ResponseBody body) {
    try {
        // todo change the file location/name according to your needs
        File futureStudioIconFile = new File(Environment.getExternalStorageDirectory() + File.separator + "/bill.pdf");

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            byte[] fileReader = new byte[4096];

            long fileSize = body.contentLength();
            long fileSizeDownloaded = 0;

            inputStream = body.byteStream();
            outputStream = new FileOutputStream(futureStudioIconFile);

            while (true) {
                int read = inputStream.read(fileReader);

                if (read == -1) {
                    break;
                }

                outputStream.write(fileReader, 0, read);

                fileSizeDownloaded += read;

                // Log.d(TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
            }

            outputStream.flush();

            return true;
        } catch (IOException e) {
            return false;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }

            if (outputStream != null) {
                outputStream.close();
            }
        }
    } catch (IOException e) {
        return false;
    }
}

这是showDownloadNotification()函数:

void showDownloadNotification() {
    try {
        Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "/invoice.pdf");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(selectedUri, "resource/folder");
        //            startActivity(intent);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_logo_winds)
                .setContentTitle("Invoice downloaded")
                .setContentText("")
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        // notificationId is a unique int for each notification that you must define
        notificationManager.notify(1, builder.build());

    } catch (Exception e) {
        Log.e(TAG, "Notification " + e.toString());
    }
}

因此,当我点击创建的通知时,什么也没有发生,并且当我取消注释startActivity(意图)时;它说无法找到意图的活动而崩溃。 如何通过单击为它创建的通知来打开已下载的文件?

1 个答案:

答案 0 :(得分:0)

似乎您将错误的数据类型传递给了showDownloadNotification()方法的意图。这是打开PDF的方法:

Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "/invoice.pdf");

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "application/pdf"); // here we set correct type for PDF
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

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