Android cannot open downloaded file

I successfully download the pdf file from the Internet and then open the download folder using the following Intent:

Intent pdfFileIntent = new Intent(); pdfFileIntent.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS); ctx.startActivity(pdfFileIntent); 

This code is located inside the onReceive () method of the BroadcastReceiver, which I created to work with download completion events. The download is completed and successful (I can see the download status in the download folder, which opens immediately after the download is completed, as expected). However, when I click on the file that I just downloaded, another dialog box appears, asking me to select the application with which the file will be opened (I can choose either Adobe Reader or Quickoffice). When I select Adobe Reader, I get the error message "Invalid document path", and when I select Quickoffice, I get the error message: "The file does not open."

This is terribly annoying, especially because the download folder and the download process and managing downloaded files are completely out of my hands - does Android know the files in the download directory when it presents them to me, since it could be that it doesn’t go through the valid path? What's happening? To make everything clear, I did not specify the path in my code - Android chose the path to save the file. And I even see that the file has a reasonable size in KB, which means that it is not empty, and it is definitely a PDF file (extension .pdf).

Please, help. Thanks in advance.

+6
source share
1 answer

And I even see that the file has a reasonable size in KB, which means that it is not empty, and it is definitely a PDF file (extension .pdf).

The fact that the file has a PDF extension does not necessarily make it a valid PDF file! Move the file to your computer and see if you can open it. You may have an error in the code performing the download, resulting in incorrect content being written to the file.

0
source

Source: https://habr.com/ru/post/945914/


All Articles