Question

I know this has been asked a few times here, but I'm not sure which way I should go. This code downloads the html file okay, but I get an IOException when trying to write the html to a file. I've tried many suggestions on sof, but none seem to work for me and I'm at a loss as it seems it should be working.

class Downloader extends AsyncTask<URL, Void, Void> {
    String site = getResources().getString(R.string.sched_hd_url);
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/directory/");
    File file = new File(dir, "file.html");

    @Override
    protected Void doInBackground(URL... urls) {
        try {
            URL url = new URL(site);
            URLConnection yc = url.openConnection();
            BufferedInputStream in = new BufferedInputStream(new URL(site).openStream());
            OutputStream out = new FileOutputStream(file);
            int total = 0;
            int count;
            byte data1[] = new byte[1024];
            while ((count = in.read(data1)) != -1) {
                out.write(data1);
                total += count;
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result) {
        progress.setVisibility(View.INVISIBLE);
        finish();
    }
}

I run this code and no file appears in the directory that I specified. The directory already exists, and I do have the permissions in my manifest. Any suggestions would be greatly appreciated.

Was it helpful?

Solution

So my problem was a couple of things. First, I want to thank those that commented. In my question, I did neglect to put in the out.close(); method. When that didn't work, I was looking at the string which held the URL I wanted to use. That had errors in it. This fixed the download problem, but I wanted to download from a place where the .html file was not in the URL (example: http://www.example.com/ instead of http://www.example.com/index.html). It worked for the latter but not the former. So instead of using URLConnection I used HttpURLConnection. Here is my working code:

class Downloader extends AsyncTask<URL, Void, Void> {
    String site = getResources().getString(R.string.sched_hd_url);
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/directory/");
    File file = new File(dir, "file.html");

    @Override
    protected Void doInBackground(URL... uri) {
        FileOutputStream out = null;
        if (file.exists()) {
            try {
                file.delete();
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            URL url = new URL(site);
            HttpURLConnection yc = (HttpURLConnection) url.openConnection();
            BufferedInputStream in = new BufferedInputStream(
                    new URL(site).openStream());
            out = new FileOutputStream(file);
            int total = 0;
            int count;
            byte data1[] = new byte[1024];
            while ((count = in.read(data1)) != -1) {
                out.write(data1);
                total += count;
            }
            in.close();
            out.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result) {
        progress.setVisibility(View.INVISIBLE);
        finish();
    }
}

Also another error in my question was in regards to no implementation checking to see if the file had already existed. Thanks again for the help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top