How to download PDF with url on sdcard? Android

0

I would like to be able to make a button that when clicked, I downloaded a pdf with the url of this one to the sdcard, but I am not getting at all ...

    
asked by anonymous 30.07.2015 / 08:55

1 answer

3

You can use the DownloadManager class of Android itself. It will already do all the work for you (including notifications with progress and success / failure):

String url = "http://suaurl.com.br/";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Alguma descrição");
request.setTitle("Algum titulo");

//A notificação de conslusão só esta disponível a partir da API 11
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}

//Salvando o arquivo no diretório de Downloads
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "MeuPdf.pdf");

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

The result will look like this:

And,whencomplete:

YoucanviewthefileintheDownloadsfolder:

    
30.07.2015 / 16:55