How to download PDF with the url in sdcard? Android

Asked

Viewed 265 times

0

I wish I could make a button that when clicked, download a pdf with the url of this to sdcard, but I’m not getting at all...

1 answer

3

You can use the class DownloadManager from 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 be like this:

Fazendo o download do arquivo

And when complete:

Download completo

You can view the file in the folder Downloads:

inserir a descrição da imagem aqui

  • 1

    AP was supposed to accept your answer, I needed to do that, and I used your answer as a basis :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.