daily pastebin goal
6%
SHARE
TWEET

Untitled

a guest Jun 21st, 2018 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static void downloadPdf(Context context, String url, String name, final Callback callback){
  2.     Uri uri;
  3.     try{
  4.         uri = Uri.parse(url);
  5.     }
  6.     catch(Exception e){
  7.         Log.e(LogTags.UI, "Error parsing pdf url " + url, e);
  8.         callback.onError();
  9.         return;
  10.     }
  11.  
  12.     DownloadManager.Request r = new DownloadManager.Request(uri);
  13.  
  14.     // This put the download in the same Download dir the browser uses
  15.     r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name);
  16.  
  17.     // When downloading music and videos they will be listed in the player
  18.     // (Seems to be available since Honeycomb only)
  19.     r.allowScanningByMediaScanner();
  20.  
  21.     // Notify user when download is completed
  22.     // (Seems to be available since Honeycomb only)
  23.     r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  24.  
  25.     // Start download
  26.     final DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
  27.     final long downloadId = dm.enqueue(r);
  28.  
  29.     BroadcastReceiver receiver = new BroadcastReceiver() {
  30.         @Override
  31.         public void onReceive(Context context, Intent intent) {
  32.             String action = intent.getAction();
  33.             if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
  34.                 if(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0) != downloadId){
  35.                     // Quick exit - its not our download!
  36.                     return;
  37.                 }
  38.  
  39.                 DownloadManager.Query query = new DownloadManager.Query();
  40.                 query.setFilterById(downloadId);
  41.                 Cursor c = dm.query(query);
  42.                 if (c.moveToFirst()) {
  43.                     int columnIndex = c
  44.                             .getColumnIndex(DownloadManager.COLUMN_STATUS);
  45.                     if (DownloadManager.STATUS_SUCCESSFUL == c
  46.                             .getInt(columnIndex)) {
  47.  
  48.                         String uriString = c
  49.                                 .getString(c
  50.                                         .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  51.                         Uri uri = Uri.parse(uriString);
  52.  
  53.                         intent = new Intent();
  54.                         intent.setAction(Intent.ACTION_VIEW);
  55.                         intent.setDataAndType(uri, "application/pdf");
  56.  
  57.                         callback.onSuccess();
  58.  
  59.                         context.startActivity(intent);
  60.  
  61.                     }
  62.                     else{
  63.                         callback.onError();
  64.                     }
  65.                 }
  66.                 else{
  67.                     callback.onError();
  68.                 }
  69.                 context.unregisterReceiver(this);
  70.             }
  71.         }
  72.     };
  73.  
  74.     context.registerReceiver(receiver, new IntentFilter(
  75.             DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top