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 ...
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 ...
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:
YoucanviewthefileintheDownloads
folder: