打开使用 Retrofit2 下载的通知点击文件
Posted
技术标签:
【中文标题】打开使用 Retrofit2 下载的通知点击文件【英文标题】:Open File on Notification click downloaded using Retrofit2 【发布时间】:2019-07-28 03:51:45 【问题描述】:我正在使用改造网络服务下载 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 (intent);它通过说找不到意图的活动而崩溃。 如何通过单击我为它创建的通知打开我下载的文件?
【问题讨论】:
你检查过developer.android.com/training/notify-user/navigation @ashazar 是的,我已经看到,无法从我的通知点击中找到我应该在哪里以及如何打开文件/文件夹 【参考方案1】:您似乎将错误的数据类型传递给意图 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);
【讨论】:
以上是关于打开使用 Retrofit2 下载的通知点击文件的主要内容,如果未能解决你的问题,请参考以下文章
Android RxJava2+Retrofit2单文件下载监听进度封装