0

I am new to android and I am trying to download a pdf file using URL link. The file is downloaded in Downloads directory and the file exists in the directory after the download operation is completed. If I try to open the pdf file using intent the app is crashing, I am quite aware that the URI which I pass to the Intent.setDataAndType() is wrong. How to get the correct URI for the downloaded file.

Here is my code.

String url = "https://…";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.setDescription("Some descrition");

request.setTitle("Some title");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.pdf");

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/name-of-the-file.pdf");

Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
context.startActivity(intent);

I am somewhere messing with the path passed to the intent. Any kind of help is appreciable. Thanks in advance.

Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
Ram Keerthy
  • 227
  • 4
  • 16

0 Answers0