peacestorm

Download file Android app

May 16th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. public void fileDownload(String url, String fileName) {
  2.  
  3.         //SET CUSTOM PATH HERE. Example:
  4.  
  5.         File direct = new File(Environment.getExternalStorageDirectory() + "/Mods");
  6.  
  7.       //The code above will save the file to /sdcard/Mods
  8.  
  9.  
  10.  
  11.       if (!direct.exists()) {
  12.  
  13.             direct.mkdirs();
  14.  
  15.        }
  16.  
  17.  
  18.  
  19.         DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
  20.  
  21.  
  22.  
  23.         Uri downloadUri = Uri.parse(url);
  24.  
  25.        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
  26.  
  27.  
  28.  
  29.         request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
  30.  
  31.                 .setAllowedOverRoaming(false).setTitle("Downloading Mod")
  32.  
  33.                 .setDestinationInExternalPublicDir("Mods", fileName);
  34.  
  35.                 //Make the string above the same as you did before. In this case, it was "/Mods"
  36.  
  37.  
  38.  
  39.         mgr.enqueue(request);
  40.  
  41. }
  42.  
  43.  
  44.  
  45. //To download a file, call
  46.  
  47. // fileDownload(URL_HERE, FILE_NAME_HERE);
  48.  
  49. //Code by someone else, I only improved it
Add Comment
Please, Sign In to add comment