Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to download a file with android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 183
    Comment on it

    Downloading in android devices i.e receiving data from remote system. In android devices Download manager is a system service that is used to download and manage any type and size of file. We have two nested classes first is DownloadManager.Query and second one is DownloadManager.Request. Query class is for filter download manager queries and request class is for necessary information to request a new download. Here is a little example to download a file.

    public void downloadurl(String url, String article_heading) {
    
    File direct = new File(Environment.getExternalStorageDirectory() + "/" + getString(R.string.app_name));
    
        if (!direct.exists()) {
            direct.mkdirs();
        }
    
        DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
    
    
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
               .setDescription("Downloading pdf " + article_heading)
               .setDestinationInExternalPublicDir("/" + getString(R.string.app_name), article_heading + ".pdf");
    
        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    
        mgr.enqueue(request);
    }
    

    When download completes download manager send an action to the Broadcast intent and onReceive method is called.

     BroadcastReceiver onComplete=new BroadcastReceiver() {
            public void onReceive(Context ctxt, Intent intent) {
    
                // Do Something
                Toast.makeText(PdfViewerActivity.this, "Article successfully downloaded", Toast.LENGTH_LONG).show();
            }
        };
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: