3

how to: download (via an http connection) and open or save a pdf file programmatically.

Say I have a button on my screen and the url to the pdf, when the button is clicked I want to download the pdf from the url and have the user presented with the choice to open or save the file. There must be a standard way to do something so commonplace, should I open the browser to the url or can I do this from my app?

tshepang
  • 12,111
  • 21
  • 91
  • 136
lost baby
  • 3,178
  • 4
  • 32
  • 53

1 Answers1

7

Like this:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("*url for your pdf*"));
startActivity(browserIntent);

This will cause the pdf to be downloaded in the notification bar, as if you had initiated the download through the browser.

Check this question if you have problems: How can I open a URL in Android's web browser from my application?

Community
  • 1
  • 1
Marmoy
  • 8,009
  • 7
  • 46
  • 74
  • Looks good, downloads the file but can I have an option to open once downloaded? Otherwise people will have to exit my app to open the file in the downloads folder. – lost baby Jul 06 '11 at 19:47
  • 1
    PS: ok, they can get at em through the notification bar like you imoplied. Hey, its my first app and first android, am I expected to know what I'm doing? ;) Thanks – lost baby Jul 06 '11 at 19:52