android – Download button does not work please help – Education Career Blog

Ok so I have my app 90% built everything works but my download button. I have been stuck on this for days now so if anyone could help a guy out with something that will work for this. my files are .pkg and I feed the filename from a xml file and here is my buttons code but I dont know what I am doing wrong here. It just does nothing upon clicking it. The appUrl is my file name from the xml file

Button b_get_book = (Button)v.findViewById(;
                new View.OnClickListener()
                    public void onClick(View v) {
                         try {
                                File root = Environment.getExternalStorageDirectory();
                                URL u = new URL("" + appUrl);
                                HttpURLConnection c = (HttpURLConnection) u.openConnection();
                                FileOutputStream f = new FileOutputStream(new File(root, appUrl));

                                InputStream in = c.getInputStream();

                                byte buffer = new byte1024;
                                int len1 = 0;
                                while ((len1 = > 0) {
                                    f.write(buffer, 0, len1);
                            } catch (Exception e) {
                                Log.d("Downloader", e.getMessage());

Any help would be great I have been pulling my hair out over this. I make a pretty complex app and its the easy thing that gets me…… go figure. if anyone can help me out with some working code from a button that would be awesome. even better if it has a progress bar for the downloads. that is my next task after getting the actual download working. Thanks


First idea is that you get an IOException when trying to create a file. If I were you I would, first of all, move the code which is currently in your try-catch clause to somewhere else, and test it. Then when it’s working well I would just call it from within try-catch.

Also, I would recommend against running such long operations in GUI thread – that makes your app non-responsive to user input. The following approach is most recommended:

        public void onClick(View v) {
            new AsyncTask<Void, Void, Void>() {
                  protected Void doInBackground(Void... params) {
                      try {
                        /* your code here*/
                      } catch (Exception e) {
                        Log.e("Downloader", e.getMessage());

Leave a Comment