0

I'm looking to securely download or view PDF and android files within an app from a URL. I'm successfully able to download and open PDF files by using the DownloadManager or opening an intent with a URI as the argument. The same code will download a doc or docx file, but when I try to open the doc \ docx file on the android emulator (directly from downloads), I get "can't open file".

This is the code that opens an intent and downloads the docx \ doc file:

public void openWebPage(String url) {
    Uri webpage = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);

    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

Do you know why I am unable to open these doc \ docx files?

trees_are_great
  • 3,881
  • 3
  • 31
  • 62
  • Presumably, there is no app on your emulator that knows how to display proprietary Microsoft document formats. Or, the MIME type from the server is incorrect, and there is no app on your emulator that knows how to display whatever that MIME type is. – CommonsWare Jun 27 '19 at 11:37
  • Ok, so to open the file from the app, would I need to rely on the user having something like google docs? – trees_are_great Jun 27 '19 at 13:39
  • 1
    Yes. This is not significantly different than a desktop OS, where you cannot assume that the user has Microsoft Office or LibreOffice or something installed. – CommonsWare Jun 27 '19 at 13:41
  • Thanks, although, I have installed and logged into google docs and I can copy the file into google docs and open it from there, but I can't just open it from the downloads folder, even when google docs is installed. This seems a little different. It makes sense though, thank you – trees_are_great Jun 27 '19 at 13:44

1 Answers1

1

Do you have an app that can display *.doc / *.docx files? If so, there might be a problem similar to what CommonsWare pointed out in his comment, or with the proper registration of the app so that it "reacts" properly to your intent.

If you don't have an app on your emulator to display this kind of file, this means there is no application registered for your particular intent, which in turn means that it fails, e.g. says "can't open file".

This is the normal and expected behaviour in case no app is registered for opening this type of file.

If you do have an app to open Microsoft Word documents, maybe try to reinstall it, which might fix any problems you might have.

So, in the end it comes down to installing / reinstalling an app to view *.doc / *.docx files. There are countless viewers for Microsoft Windows documents in the app store. So if you are able to access the Play Store from within your emulator, go ahead :)

If this still doesn't fix your error, try to further explain your issue, e.g. which app you used and at which point exactly it fails. As far as I can tell, your intent is fine, you just don't have an app to display whatever kind of document you want to open.

th0bse
  • 97
  • 8
  • Is there a way to open it from my app? This answer suggests there is a way: https://stackoverflow.com/questions/10242306/android-how-to-open-a-doc-extension-file – trees_are_great Jun 27 '19 at 13:05
  • 1
    Yes, there is. Essentially, you are doing the right thing with the Intent you are using. I can't see which answer specifically you are referring to, as you only linked the question itself, but as I already said, you need some kind of external application to view the files, because android itself can't render Microsoft Word files. An Intent basically is a kind of broadcast "hey, I have something to do, who can do this?", so you need an app which can respond to this, e.g. is capable of displaying *.doc / *.docx files. – th0bse Jun 27 '19 at 14:04
  • An intent is more than this though, but in your case you can think of it like this. – th0bse Jun 27 '19 at 14:05
  • 1
    The top answer in the question you linked, (https://stackoverflow.com/a/10242471/10891659) gives the perfect solution for this, as far as I'm aware.. Haven't tested it myself, but it looks pretty accurate for your case. – th0bse Jun 27 '19 at 14:08